使用 itertools.product 解压列表的说明

Explanation on unpacking a list with itertools.product

我似乎无法理解解包 (*) 和 itertools.product() 如何处理下面的示例。

for x in product(["ghi","abc"]):
    print(x)

输出:

('ghi',)
('abc',)

并使用 *

for x in product(*["ghi","abc"]):
    print(x)

输出:

('g', 'a')
('g', 'b')
('g', 'c')
('h', 'a')
('h', 'b')
('h', 'c')
('i', 'a')
('i', 'b')
('i', 'c')

这个输出是如何产生的?我知道 product() 通常会根据 'repeat' 的数量生成组合。至于上面,repeat默认为1,怎么没有生成('a','g')?

我猜想 *["ghi","abc"] 实际上对 product() 函数产生了什么?我的意思是我看到了结果是什么,但我似乎无法理解它是如何工作的。

当您将参数解压缩到 product 时,这与手动输入列表中的每个元素作为参数是一样的。即:

product(*[a, b]) == product(a, b)

所以您真正要做的是将列表中的每个字符串传递给 product()。 Python 只是在幕后为您做这件事。

('a', 'g') 没有出现在结果中的原因仅仅是因为产品的工作原理。 The official documentation of product does a good job of explaining how exactly it works:

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:

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)