[英]Generate all combinations from multiple lists in python
我有一个数据集,其中包含多个列表,每个列表具有不同数量的元素。 例如。
list1 = ['a','b','c']
list2 = ['d','e']
list3 = ['f','g','h','i']
我想从这些列表中生成所有可能的唯一组合,条件如下:
我该怎么做呢?
我认为这会做到(其中很多步骤可以组合,但保留它们以显示步骤)
创建一个包含所有项目的新列表
list4 = list1 + list2 + list3
还有另一个列表来遍历它们以找到所有 5 种组合(您没有指定订单或更换,因此请在此处阅读并根据需要进行更改)
list5 = list(itertools.combinations(list4, 5))
然后从每个列表中删除没有元素的项目。
[i for i in list5 if any(j in list1 for j in i) and any(j in list2 for j in i) and any(j in list3 for j in i)]
所以在一行中它可能是:
[i for i in itertools.combinations(list1 + list2 + list3, 5) if any(j in list1 for j in i) and any(j in list2 for j in i) and any(j in list3 for j in i)]
from itertools import product
Suit = [u"\u2666", u"\u2665", u"\u2663", u"\u2660"]
Card = ['A','K','Q','J','10','9','8','7','6','5','4','3','2']
FullDeck = list(product(Card, Suit))
for i in FullDeck:
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.