Message372392
In the documentation the following example is given:
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)
The proposed enhancement uses a nested generator so no intermediate results are created.
def product2(*args, repeat=1):
def concat(result, pool):
yield from (x+[y] for x in result for y in pool)
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = concat(result, pool)
for prod in result:
yield (tuple(prod)) |
|
Date |
User |
Action |
Args |
2020-06-25 23:11:22 | ataher | set | recipients:
+ ataher, docs@python |
2020-06-25 23:11:22 | ataher | set | messageid: <1593126682.32.0.703433792229.issue41120@roundup.psfhosted.org> |
2020-06-25 23:11:22 | ataher | link | issue41120 messages |
2020-06-25 23:11:22 | ataher | create | |
|