【问题标题】:Python: How to return a list of distinct items with counts from a list of lists that includes duplicates?Python:如何从包含重复项的列表列表中返回具有计数的不同项目列表?
【发布时间】:2019-03-07 20:43:52
【问题描述】:

我有嵌套在列表中的列表。这是我尝试使用列表的迷你预览来绘制单个嵌套列表的计数的代码:

count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
    distinct_list = []
    count_list.sort()
    for x in count_list:
        if x not in distinct_list: 
            distinct_list.append(x)       
    for z in distinct_list:
        for y in count_list:
            if y == z:
                print(z, count_list.count(z))

我试图让输出成为一个字典/列表,其中只有不同的嵌套列表及其计数,以便我可以按计数对结果进行排序。列表不采用两个值,因此我正在考虑将其作为字典以便于存储。 这些是我从上面的代码中得到的结果。

(['a', 'b'], 2)
(['a', 'b'], 2)
(['c'], 2)
(['c'], 2)
(['d', 'e'], 1)
(['e'], 1)

我希望输出只为每个不同的列表及其各自的计数提供一行(而不是在上面的当前输出中看到的同一列表的多行)。有什么想法吗?

【问题讨论】:

  • 您的输出项看起来像元组,您是否特别想要一个字典,或者您只需要某种方式来对您的列表和计数进行分组(因此元组或其他类型可以正常工作)?
  • 我只是在寻找一种对列表进行分组的方法。我不介意另一种方式。

标签: python list loops dictionary nested-lists


【解决方案1】:

如果您可以将嵌套列表转换为元组以便将它们用作键,那么将结果作为 dict(与您关于如何解决此问题的初始想法之一内联)也将相当简单。例如:

data = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]

results = {}
for d in data:
    t = tuple(d)
    if t not in results:
        results[t] = data.count(d)

print(results)

# OUTPUT
# {('a', 'b'): 2, ('d', 'e'): 1, ('e',): 1, ('c',): 2}

【讨论】:

    【解决方案2】:

    如果你转换成元组,你可以使用字典理解

    count_list = [tuple(i) for i in count_list]
    
    d = {i: count_list.count(i) for i in set(count_list)}
    
    {('a', 'b'): 2, ('d', 'e'): 1, ('c',): 2, ('e',): 1}
    

    【讨论】:

      【解决方案3】:

      如果您不想使用任何库,即使 Counter 是 python 原生的,您也可以这样做...

      arr = [[1, 2], [3], [1, 2], [4], [3]]
      new_arr = []
      for elem in arr:
          # You are wrapping your element in a list, at the same scale
          # than the list you wish to output (meaning 3 dimensions depth list).
          # In other words, you have a list... that contains a list, containing
          # your element and its count. The first depth seems useless at first glance,
          # but it's just here to keep the same depth you will have appending to new_arr.
      
          # You can try by yourself doing "obj = [elem, arr.count(elem)]" if you don't
          # believe me.
          obj = [ [elem, arr.count(elem)] ]
          if obj[0] not in new_arr:
              # checking on first index to check our elem and not our ""wrapper""
              new_arr += obj
      
      print(new_arr)
      # [[[1, 2], 2], [[3], 2], [[4], 1]]
      

      【讨论】:

        【解决方案4】:

        你应该使用collections.Counter():

        >>> count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
        >>> import collections
        >>> result = collections.Counter(tuple(x) for x in count_list)
        >>> print(result)
        Counter({
            ('c',): 2,
            ('a', 'b'): 2,
            ('d', 'e'): 1,
            ('e',): 1
        })
        

        【讨论】:

        • 这必然有可散列的键(不是列表)。
        • @Alex 是的!没问题,只需将任何内容转换为可散列
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多