我想在排列和组合之间产生一些元组列表。例如,如果我有列表
list_of_tuples = [(1,20), (1,21), (2,18), (2,19)]
我想创造所有可能的"组合" 3个元组,以便我希望列表包含结果[(1,20), (1,20), (1,20)]
,但我认为[(1,20), (1,20), (1,21)]
与[(1,20), (1,21), (1,20)]
和[(1,21), (1,20), (1,20)]
相同,并且只想保留其中一个(与哪一个无关。
换句话说,如果"组合"包含与另一个"组合相同的元组",我不想保留另一个元组。
我尝试过像
这样的事情list_of_lists = [list_of_tuples]*3
results = list(itertools.product(*list_of_lists))
results = set(results)
但是使用set()
我会丢失[(1,20), (1,20), (1,20)]
以及所有其他具有相同元组三次的结果。
答案 0 :(得分:4)
使用itertools.combinations_with_replacement
,它应该产生你所描述的内容:
>>> from itertools import combinations_with_replacement
>>> list_of_tuples = [(1,20), (1,21), (2,18), (2,19)]
>>> list(combinations_with_replacement(list_of_tuples, 3))
[((1, 20), (1, 20), (1, 20)),
((1, 20), (1, 20), (1, 21)),
((1, 20), (1, 20), (2, 18)),
((1, 20), (1, 20), (2, 19)),
((1, 20), (1, 21), (1, 21)),
((1, 20), (1, 21), (2, 18)),
((1, 20), (1, 21), (2, 19)),
((1, 20), (2, 18), (2, 18)),
((1, 20), (2, 18), (2, 19)),
((1, 20), (2, 19), (2, 19)),
((1, 21), (1, 21), (1, 21)),
((1, 21), (1, 21), (2, 18)),
((1, 21), (1, 21), (2, 19)),
((1, 21), (2, 18), (2, 18)),
((1, 21), (2, 18), (2, 19)),
((1, 21), (2, 19), (2, 19)),
((2, 18), (2, 18), (2, 18)),
((2, 18), (2, 18), (2, 19)),
((2, 18), (2, 19), (2, 19)),
((2, 19), (2, 19), (2, 19))]
答案 1 :(得分:0)
你也可以试试这个:
from itertools import product
from collections import Counter
list_of_tuples = [(1,20), (1,21), (2,18), (2,19)]
list_of_lists = [list_of_tuples] * 3
seen = set()
unique = []
for prod in product(*list_of_lists):
curr = frozenset(Counter(prod).items())
if curr not in seen:
seen.add(curr)
unique.append(prod)
print(unique)
哪个输出:
[((1, 20), (1, 20), (1, 20)),
((1, 20), (1, 20), (1, 21)),
((1, 20), (1, 20), (2, 18)),
((1, 20), (1, 20), (2, 19)),
((1, 20), (1, 21), (1, 21)),
((1, 20), (1, 21), (2, 18)),
((1, 20), (1, 21), (2, 19)),
((1, 20), (2, 18), (2, 18)),
((1, 20), (2, 18), (2, 19)),
((1, 20), (2, 19), (2, 19)),
((1, 21), (1, 21), (1, 21)),
((1, 21), (1, 21), (2, 18)),
((1, 21), (1, 21), (2, 19)),
((1, 21), (2, 18), (2, 18)),
((1, 21), (2, 18), (2, 19)),
((1, 21), (2, 19), (2, 19)),
((2, 18), (2, 18), (2, 18)),
((2, 18), (2, 18), (2, 19)),
((2, 18), (2, 19), (2, 19)),
((2, 19), (2, 19), (2, 19))]
答案 2 :(得分:0)
你看起来像这样吗?
list_of_tuples = [(1,20), (1,21), (2,18), (2,19)]
import itertools
data=[]
for i in itertools.product(list_of_tuples,repeat=3):
data.append(i)
dict_1=[]
for i in data:
if sorted(i,key=lambda x:x[1]) not in dict_1:
dict_1.append(sorted(i,key=lambda x:x[1]))
print(dict_1)
输出:
[[(1, 20), (1, 20), (1, 20)], [(1, 20), (1, 20), (1, 21)], [(2, 18), (1, 20), (1, 20)], [(2, 19), (1, 20), (1, 20)], [(1, 20), (1, 21), (1, 21)], [(2, 18), (1, 20), (1, 21)], [(2, 19), (1, 20), (1, 21)], [(2, 18), (2, 18), (1, 20)], [(2, 18), (2, 19), (1, 20)], [(2, 19), (2, 19), (1, 20)], [(1, 21), (1, 21), (1, 21)], [(2, 18), (1, 21), (1, 21)], [(2, 19), (1, 21), (1, 21)], [(2, 18), (2, 18), (1, 21)], [(2, 18), (2, 19), (1, 21)], [(2, 19), (2, 19), (1, 21)], [(2, 18), (2, 18), (2, 18)], [(2, 18), (2, 18), (2, 19)], [(2, 18), (2, 19), (2, 19)], [(2, 19), (2, 19), (2, 19)]]