New submission from Abbas Taher <altaher_ab...@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@python
components: Documentation
files: product example.py
messages: 372392
nosy: ataher, docs@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue41120>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to