【问题标题】:Unique permutations for a list of tuples元组列表的唯一排列
【发布时间】:2018-01-23 12:42:12
【问题描述】:

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

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

【问题讨论】:

    标签: python combinatorics nested-lists


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你也可以试试这个:

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

      【讨论】:

        【解决方案3】:

        你看起来像这样吗?

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-09
          • 1970-01-01
          • 1970-01-01
          • 2021-11-23
          • 2017-04-12
          • 2020-12-15
          • 1970-01-01
          相关资源
          最近更新 更多