Python itertools.product with variable number of arguements

空扰寡人 提交于 2019-12-10 13:05:16

问题


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

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.

Thanks for any suggestions.


回答1:


You need to use * to separate the single list into its constituent lists:

searchterms = list(itertools.product(*lists))

See the Python Tutorial section on Unpacking Argument Lists.



来源:https://stackoverflow.com/questions/10148834/python-itertools-product-with-variable-number-of-arguements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!