Created
September 29, 2018 09:08
-
-
Save cdarlint/34fdcf2f836890df3d8a3e6a026e1373 to your computer and use it in GitHub Desktop.
list all cross product of list of list
This file contains hidden or 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 product(b): | |
result=[] | |
for i in b[0]: | |
if len(b)>1: | |
xx=product(b[1:]) | |
result+=[[i]+v for v in xx] | |
else: | |
result+=[[i]] | |
return result | |
p=product([[1,2,3],[7,8],[9,4],[5,6]]) | |
print(p) | |
# OUTPUT | |
# [[1, 7, 9, 5], [1, 7, 9, 6], [1, 7, 4, 5], [1, 7, 4, 6], [1, 8, 9, 5], [1, 8, 9, 6], [1, 8, 4, 5], [1, 8, 4, 6], [2, 7, 9, 5], [2, 7, 9, 6], [2, 7, 4, 5], [2, 7, 4, 6], [2, 8, 9, 5], [2, 8, 9, 6], [2, 8, 4, 5], [2, 8, 4, 6], [3, 7, 9, 5], [3, 7, 9, 6], [3, 7, 4, 5], [3, 7, 4, 6], [3, 8, 9, 5], [3, 8, 9, 6], [3, 8, 4, 5], [3, 8, 4, 6]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment