【问题标题】:How to generate all distinct combinations (where input elements are repeated) in Python(using Itertools)?如何在 Python(使用 Itertools)中生成所有不同的组合(重复输入元素)?
【发布时间】:2019-04-30 00:04:33
【问题描述】:

我有一组数字[1, 2, 4, 1]。现在,我想从这组大小 k 中生成所有可能的组合(例如 k = 3)。所有生成的输出集不得重复

示例:[1, 2, 1][2, 1, 1] 是相同的集合,但不应选择它们。只有其中一个应该出现。是否可以在 Python 中使用来自 itertools 的组合?

import itertools
x = [1, 2, 1]
print([p for p in itertools.product(x, repeat=3)])

我尝试过使用 itertools.product 但它不起作用并且 使用来自 itertools 的组合得到重复

我尝试过使用 itertools.combinations print([p for p in set(itertools.combinations(x, r=3))])

如果我给出以下输入

  x =  [-1, 0, 1, 2, -1, -4]

为 r = 3 生成的输出是

[(0, -1, -4), (-1, -1, -4), (-1, 1, -4), (0, 2, -1), (-1, 0, 2), (-1, 2, -4), (0, 1, 2), (2, -1, -4), (-1, 0, -1), (0, 1, -4), (1, 2, -4), (-1, 0, 1), (-1, 1, 2), (0, 2, -4), (-1, 1, -1), (-1, 2, -1), (1, 2, -1), (0, 1, -1), (-1, 0, -4), (1, -1, -4)]

(-1, 0, 1)(0, 1, -1) 是具有相同组合的重复集。我不确定如何克服这个问题。

【问题讨论】:

    标签: python combinations permutation itertools


    【解决方案1】:

    这些被称为multisets,我们可以通过sympy模块轻松获得它们的组合。

    from sympy.utilities.iterables import multiset_combinations
    
    list(multiset_combinations([1, 2, 4, 1], 3))
    [[1, 1, 2], [1, 1, 4], [1, 2, 4]]
    

    这是@EdedkiOkoh 的示例:

    x = [-1, 0, 1, 2, -1, -4]
    list(multiset_combinations(x, 3))
    [[-4, -1, -1],
        [-4, -1, 0],
        [-4, -1, 1],
        [-4, -1, 2],
        [-4, 0, 1],
        [-4, 0, 2],
        [-4, 1, 2],
        [-1, -1, 0],
        [-1, -1, 1],
        [-1, -1, 2],
        [-1, 0, 1],
        [-1, 0, 2],
        [-1, 1, 2],
        [0, 1, 2]]
    

    【讨论】:

      【解决方案2】:

      您可以使用 python 的 set datatype 删除这些重复项,因为集合仅包含唯一组合:

      import itertools as it
      x = [-1, 0, 1, 2, -1, -4]
      
      permutations = [p for p in set(it.combinations(x, r=3))]
      print(permutations)
      

      输出:

      [(0, 1, 2),
       (-1, 1, -1),
       (-1, 2, -1),
       (0, -1, -4),
       (-1, -1, -4),
       (-1, 1, -4),
       (-1, 2, -4),
       (2, -1, -4),
       (1, 2, -4),
       (-1, 0, 1),
       (1, 2, -1),
       (-1, 0, -4),
       (-1, 0, 2),
       (-1, 0, -1),
       (-1, 1, 2),
       (0, 2, -4),
       (0, 2, -1),
       (0, 1, -4),
       (1, -1, -4),
       (0, 1, -1)]
      

      然后使用可以使用以下行:

      unique_permutations = set(tuple(sorted(t)) for t in permutations)
      

      输出:

      {(-4, -1, -1),
       (-4, -1, 0),
       (-4, -1, 1),
       (-4, -1, 2),
       (-4, 0, 1),
       (-4, 0, 2),
       (-4, 1, 2),
       (-1, -1, 0),
       (-1, -1, 1),
       (-1, -1, 2),
       (-1, 0, 1),
       (-1, 0, 2),
       (-1, 1, 2),
       (0, 1, 2)}
      

      【讨论】:

        【解决方案3】:

        如何获取组合,然后通过使用组合的frozenset 结果键入字典来仅获取唯一的组合。这只会使用生成器,直到创建 dictionary

        combs1, combs2 = itertools.tee(itertools.combinations(x, r=3))
        res = list(dict(zip(map(frozenset, combs1), combs2)).values())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-15
          • 2018-03-15
          相关资源
          最近更新 更多