繁体   English   中英

元组列表的唯一排列

[英]Unique permutations for a list of tuples

我想在排列和组合之间生成一些元组列表。 例如,如果我有列表

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)]和所有其他具有相同元组的结果三次。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM