0

itertools文档提供了以下伪代码:

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)

map(tuple, args)看起来多余:可以简单地使用 args。我错过了什么吗?

这是我的测试代码(python 2.7):

def product2(*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 = 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)

print list (product(['A','B'],['C','D'])) == list (product2(['A','B'],['C','D']))
print list (product(['A','B'],['C','D'], repeat=2)) == list (product2(['A','B'],['C','D'], repeat=2))
print list (product([],[], repeat=2)) == list (product2([],[], repeat=2))
print list (product([])) == list (product2([]))

True

True

True

True
4

1 回答 1

4

让我们传入迭代器,看看会发生什么:

>>> list(product(iter('AB'), iter('CD')))
[('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D')]
>>> list(product2(iter('AB'), iter('CD')))
[('A', 'C'), ('A', 'D')]
>>> list(product(iter('AB'), iter('CD'))) == list(product2(iter('AB'), iter('CD')))
False

结论:您需要将参数转换为元组以捕获迭代器的所有值。

当只使用一个迭代器和重复选项时,更容易说明:

>>> list(product(iter('ABC'), repeat=2))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
>>> list(product2(iter('ABC'), repeat=2))
[]
于 2012-07-03T10:37:10.557 回答