Skip to main content
 首页 » 编程设计

具有任意数量集合的 Python itertools.product

2024年08月28日36pengyingh

我想执行以下代码:

temp = [] 
temp.append([1,2]) 
temp.append([3,4]) 
temp.append([5,6]) 
 
print list(itertools.product(temp[0],temp[1],temp[2])) 

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

print list(itertools.product(temp)) 

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

请您参考如下方法:

print list(itertools.product(*temp)) 

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