source code of product
I found here: https://docs.python.org/3/library/itertools.html#itertools.product
is:
def product(*args, repeat=1):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
I simplified it for my needs as:
def product(self, lists):
tuples = [tuple(lst) for lst in lists]
res = [[]]
for tup in tuples:
# for y in tup:
# for x in res:
# res.append(x+[y])
res = [x+[y] for x in res for y in tup]
return res
I want to also take out res = [x+[y] for x in res for y in tup]
out of this 1-liner.
How do I convert it to old-fashioned for
loop?
My attempt is in comment above. Tried also to swap between tup
and res
but in both attempts Memory Limit Exceeded
(in Leetcode)
Thank you