I came across this nice line of python code for cartesian product of n number of lists that I would like to bring back on multiple lines :
def cart_product_1(*seqs):
if not seqs:
return [[]]
else:
return [[x] + p for x in seqs[0] for p in cart_product_1(*seqs[1:])]
This seemed pretty straightforward to me, but obviously I'm missing something here. I'm thinking I need to append a list somewhere but can't quite figure it out.
def cart_product_1(result,*seqs):
if not seqs:
return [[]]
else:
for x in seqs[0]:
for p in cart_product_1(result,*seqs[1:]):
result.append([x]+p)
return result
This leads to a MemoryError.
return[[x]+p]try doingprodList.append([x]+p)and return the prodList after the two loops. Don't forget to initialize prodList in the beginning.