如何在Python中创建返回此列表所有组合的函数?

时间:2018-06-21 04:34:03

标签: python for-loop recursion

我有这样的列表:

[['I'],['want','love','like'],['cat',dog]]

我想创建一个函数,可以返回列表中所有单词的组合,如下所示:

[['I'],['want'],['cat']]
[['I'],['love'],['cat']]
[['I'],['like'],['cat']]
[['I'],['want'],['dog']]
[['I'],['love'],['dog']]
[['I'],['like'],['dog']]

PS:该功能必须可以使用任何n个单词

1 个答案:

答案 0 :(得分:5)

import itertools
for k in itertools.product(*lst):
    print(k)