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