元组列表的唯一排列

Unique permutations for a list of tuples

我想为元组列表生成介于排列和组合之间的东西。例如,如果我有列表

list_of_tuples = [(1,20), (1,21), (2,18), (2,19)]

我想创建 3 个元组的所有可能 "combinations",以便我希望列表包含结果 [(1,20), (1,20), (1,20)],但我认为 [(1,20), (1,20), (1,21)] 与 [=14= 相同] 和 [(1,21), (1,20), (1,20)] 并且只想保留其中之一(无论是哪一个)。

换句话说,如果 "combination" 包含与另一个 "combination" 相同的元组,我不想保留另一个。

我试过

list_of_lists = [list_of_tuples]*3
results = list(itertools.product(*list_of_lists))
results = set(results)

但是通过使用 set() 我失去了 [(1,20), (1,20), (1,20)] 和所有其他具有相同元组三次的结果。

使用 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))]

你也可以试试这个:

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))]

你看起来像这样吗?

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)]]