mirror of
https://github.com/python/cpython.git
synced 2025-11-20 10:57:44 +00:00
Add repeat keyword argument to itertools.product().
This commit is contained in:
parent
69e1309fd4
commit
18750ab2a0
1 changed files with 7 additions and 3 deletions
|
|
@ -340,7 +340,7 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. function:: product(*iterables)
|
.. function:: product(*iterables[, repeat])
|
||||||
|
|
||||||
Cartesian product of input iterables.
|
Cartesian product of input iterables.
|
||||||
|
|
||||||
|
|
@ -353,11 +353,15 @@ loops that truncate the stream.
|
||||||
so that if the inputs iterables are sorted, the product tuples are emitted
|
so that if the inputs iterables are sorted, the product tuples are emitted
|
||||||
in sorted order.
|
in sorted order.
|
||||||
|
|
||||||
|
To compute the product of an iterable with itself, specify the number of
|
||||||
|
repetitions with the optional *repeat* keyword argument. For example,
|
||||||
|
``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
|
||||||
|
|
||||||
Equivalent to the following except that the actual implementation does not
|
Equivalent to the following except that the actual implementation does not
|
||||||
build-up intermediate results in memory::
|
build-up intermediate results in memory::
|
||||||
|
|
||||||
def product(*args):
|
def product(*args, **kwds):
|
||||||
pools = map(tuple, args)
|
pools = map(tuple, args) * kwds.get('repeat', 1)
|
||||||
if pools:
|
if pools:
|
||||||
result = [[]]
|
result = [[]]
|
||||||
for pool in pools:
|
for pool in pools:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue