我有两个生成itertools生成器的列表,如下所示:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
import itertools
def all_combinations(any_list):
return itertools.chain.from_iterable(
itertools.combinations(any_list, i + 1)
for i in range(len(any_list)))
combinationList1 = all_combinations(list1)
combinationList2 = itertools.combinations(list2, 2)
通过以下代码,我可以找到这些组合:
for j in combinationList1:
print(j)
现在我想把combinationList1
和combinationList2
中的所有可能组合起来,这样,所需的输出将是:[1,a,b],[1,a,c],…,[1,2,3,a,b],[1,2,3,a,b],[1,2,3,a,c],[1,2,3,b,c]。你知道吗
我不能从itertools组合中列出一个列表,因为真正的数据集列表要大得多。有人想知道如何把两种工具结合起来吗?你知道吗
如果要迭代组合,可以执行product+chain:
输出
相关问题 更多 >
编程相关推荐