2

我希望执行以下代码:

temp = []
temp.append([1,2])
temp.append([3,4])
temp.append([5,6])

print list(itertools.product(temp[0],temp[1],temp[2]))

但是,我想对任意长度的 temp 执行它。即更像是:

print list(itertools.product(temp))

如何在不明确知道 temp 中有多少条目的情况下为 itertools.product 正确格式化输入以在第一段代码中产生相同的结果?

4

2 回答 2

3
print list(itertools.product(*temp))

用于*将可迭代的参数解包为单独的位置参数。

于 2013-08-20T17:54:16.630 回答
0

或者可以这样做:

使用 zip 组合列表。结果是一个列表迭代器。

a = ["A", "a"]
b = ["B", "b"]
c = ["C", "c"]

number_iterator = zip(a,b,c)
numbers = list(number_iterator)

print (numbers)
于 2018-08-13T18:43:56.577 回答