【问题标题】:How to get unique combination value from list of list in python如何从python中的列表列表中获取唯一的组合值
【发布时间】:2019-05-07 19:28:49
【问题描述】:

我有一个类似列表的列表

items = [[1, 2], [2, 3], [2, 1], [1, 2]]

只需要从集合中提取唯一的组合,不重复。例如 [1,2] 和 [2,1] 是相同的。在这种情况下,我们只需要考虑一组。

预期输出:[[1,2],[2,3]]

如何使用 python 实现这一点?

【问题讨论】:

标签: python python-2.7 unique


【解决方案1】:
items = [[1, 2], [2, 3], [2, 1], [1, 2]]
result = {tuple(sorted(c)) for c in items}
print(result)  # {(1, 2), (2, 3)}

请注意这如何影响结果中组合的顺序和组合中的数字。

【讨论】:

  • 不错的解决方案,假设允许results 包含items 中不存在的值。例如,如果items[[2,1]],那么result{(1,2)}
【解决方案2】:

得到你想要的最少的代码是这样的。如果您必须将结果作为列表列表,则需要一个包装器

set(frozenset(i) for i in items)

或者如果不能保证列表中的每个项目在该列表中都是唯一的,

set(tuple(sorted(i)) for i in items)

【讨论】:

    【解决方案3】:

    您可以将所有内部列表冻结到frozensets 并制作一组外部列表。

    def deep_frozenset(container):
        """Freeze all lists into frozensets."""
    
        if isinstance(container, list):
            return frozenset(deep_frozenset(el) for el in container)
        else:
            return container
    
    result = deep_frozenset(items)
    assert result == frozenset({frozenset({2, 3}), frozenset({1, 2})})
    

    您甚至可以写一个deep_thawset 来撤消操作。

    def deep_thawset(container):
        """Thaw all frozensets back into lists."""
    
        is isinstance(container, frozenset):
            return [deep_thawset(el) for el in container]
        else:
            return container
    

    您甚至可以编写往返程序。

    def roundtrip(container):
        """Find all unique combinations.
    
        >>> roundtrip([[1, 2], [2, 3], [2, 1], [1, 2]])
        [[1, 2], [2, 3]]
        """
    
        deep_thawset(deep_frozenset(container))
    

    【讨论】:

    • 我们也可以像这样定义 map(list, set(frozenset(i) for i in items)) // [[1, 2], [2, 3]]
    • @Sivachandran 是的,我的解决方案过于规范(开箱即用支持列表列表)
    • 虽然注意map(...)不返回列表,所以实际上是list(map(...))
    • map(..) 返回列表。>>> res = map(list, set(frozenset(i) for i in items)) >>> type(res)
    • @Sivachandran 啊,你在 Python2 中。你这样标记你的问题——我的错!好吧,当您从过去加入我们时,map 将不再返回列表;)
    【解决方案4】:

    您可以使用一组frozensets 进行查找:

    items = [[1, 2], [2, 3], [2, 1], [1, 2]]
    
    result = []
    lookup = set()
    
    for subl in items:
        fs = frozenset(subl)
        if fs not in lookup:
            result.append(subl)
            lookup.add(fs)
    
    print(result)
    # [[1, 2], [2, 3]]
    

    【讨论】:

      【解决方案5】:

      你可能想要一组frozensets:

      items = set(frozenset(item) for item in [[1, 2], [2, 3], [2, 1], [1, 2]])
      items
      {frozenset({1, 2}), frozenset({2, 3})}
      

      Frozensets 是不可变的,这使得它们可以散列,这是用作集合元素所必需的。

      最后,为了准确地达到您想要的输出,从您的集合中创建列表:

      list(list(item) for item in items)
      [[1, 2], [2, 3]]
      

      【讨论】:

      • list(list(item) for item in items) - 输出为 [[1, 2], [2, 3], [2, 1], [1, 2]]
      • @Sivachandran 不适用于items 的上述值。
      【解决方案6】:
      items = [[1, 2], [2, 3], [2, 1], [1, 2]]
      r=[list(i) for i in list({tuple(sorted(c)) for c in items})]
      print(r) # [[1, 2], [2, 3]]
      

      items = [[1, 2], [2, 3], [2, 1], [1, 2]]    
      u = [list(x) for x in set(tuple(x) for x in list( sorted(c) for c in items))]    
      print(u) # [[1, 2], [2, 3]]
      

      items = [[1, 2], [2, 3], [2, 1], [1, 2]]
      d = list(list(x) for x in { (v,k) if k>=v else (k,v) for k, v in items  })
      print(d) # [[1, 2], [2, 3]]
      

      【讨论】:

      • 这会打印 [[1, 2], [2, 3], [2, 1]] 。 [2,1] 又是重复组合
      猜你喜欢
      • 1970-01-01
      • 2013-08-14
      • 2014-11-20
      • 2021-06-06
      • 1970-01-01
      • 2012-10-06
      • 2012-10-05
      • 2015-04-26
      • 1970-01-01
      相关资源
      最近更新 更多