[New-bugs-announce] [issue41120] Possible performance improvement for itertools.product example on Python Docs
Abbas Taher
report at bugs.python.org
Thu Jun 25 19:11:22 EDT 2020
New submission from Abbas Taher <altaher_abbas at yahoo.com>:
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))
----------
assignee: docs at python
components: Documentation
files: product example.py
messages: 372392
nosy: ataher, docs at python
priority: normal
severity: normal
status: open
title: Possible performance improvement for itertools.product example on Python Docs
type: enhancement
Added file: https://bugs.python.org/file49262/product example.py
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41120>
_______________________________________
More information about the New-bugs-announce
mailing list