Created
April 24, 2009 11:46
-
-
Save arsatiki/101081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cartesian(*lists): | |
if not lists: | |
yield () | |
return | |
for item in lists[0]: | |
for tup in cartesian(*lists[1:]): | |
yield (item,) + tup | |
# >>> list(cartesian('abc', [1, 2])) | |
# [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment