Skip to content

Instantly share code, notes, and snippets.

@cdarlint
Created September 29, 2018 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdarlint/34fdcf2f836890df3d8a3e6a026e1373 to your computer and use it in GitHub Desktop.
Save cdarlint/34fdcf2f836890df3d8a3e6a026e1373 to your computer and use it in GitHub Desktop.
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