Skip to content

Instantly share code, notes, and snippets.

@cdarlint

cdarlint/product.py

Created Sep 29, 2018
Embed
What would you like to do?
list all cross product of list of list
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