相当于迭代器 参考itertools — Functions creating iterators for efficient looping¶
itertools.product(*iterables[, repeat])
Cartesian product of input iterables.
Roughly equivalent to nested for-loops in a generator expression. For example, product(A, B)
returns the same as ((x,y) for x in A for y in B)
.
The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted 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)
.
This function is roughly equivalent to the following code, except that the actual implementation does not build up intermediate results in memory:
>>> itertools.product(range(5), range(5))
<itertools.product object at 0x000001ED3FA23B40>
def product(*args, **kwds):
# 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 = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)