争论次数可变的Python itertools.product [英] Python itertools.product with variable number of arguements
本文介绍了争论次数可变的Python itertools.product的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个模块,以使用itertools.product组合可变数量的列表.
I am trying to write a module to combine a variable number of lists using itertools.product.
我能得到的最接近的是:
The closest I can get is:
import itertools
lists = [["item1","item2"],["A","b","C"], ["etc..."]]
searchterms = list(itertools.product(lists))
print searchterms
这不起作用,因为列表是单个列表,所以它只返回原始序列.但是我不知道如何将列表变量的每个元素传递给itertools.
This doesn't work, because lists is a single list, so it just returns the original sequence. But I can't figure out how to pass each element of the lists variable to itertools.
谢谢您的建议.
推荐答案
您需要使用*
将单个列表分成其组成列表:
You need to use *
to separate the single list into its constituent lists:
searchterms = list(itertools.product(*lists))
请参见解压缩参数列表上的Python教程部分.
See the Python Tutorial section on Unpacking Argument Lists.
这篇关于争论次数可变的Python itertools.product的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文