【问题标题】:Count the amount of lists in a list (nested list) for each length Python计算每个长度Python的列表(嵌套列表)中的列表数量
【发布时间】:2020-08-17 04:40:47
【问题描述】:

我有一个包含字符串的列表列表。像下面这样:

[['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple']]

此列表中有大约 2000 个列表,所有列表都包含不同数量的字符串。我想看看这个列表中有多少特定长度的子列表。像下面这样:

长度 2 个字符串:70 个列表 长度 3 个字符串:45 个列表 等等。

执行此操作的一种逻辑方法(我认为)是针对所需长度制作一个循环,然后针对我想要的列表数量的所有长度播放此循环。

我会想象它是这样的:

def countList(lst, x): 
    count = 0
    for i in range(len(lst)): 
        if x in lst[i]: 
            count+= 1

    return count

x = .....

但我不确定,因为我不知道如何让它计算数量。

如果有人可以帮助我,那就太好了!

【问题讨论】:

  • 能否让您的预期输出更清晰?

标签: python list count nested


【解决方案1】:

内置的collections.Counter 可以优雅地处理这个问题:

>>> from collections import Counter
>>> mydata = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple']]
>>> Counter(map(len, mydata))
Counter({3: 1, 2: 1, 6: 1})
>>> Counter(len(sublist) for sublist in mydata) # or with a generator expression
Counter({3: 1, 2: 1, 6: 1})

【讨论】:

    【解决方案2】:

    您可以将长度传递给collections.Counter,例如:

    from collections import Counter
    
    l = [['apple', 'pear', 'apple'], ['apple', 'apple'],['apple', 'apple'],['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'], ['apple', 'pear', 'apple']]
    
    counts = Counter(map(len,l))
    

    并获取字典counts like:

    Counter({3: 2, 2: 3, 6: 1})
    

    长度为3的有2个,长度为2的有3个,长度为6的有1个。

    您可以像访问任何字典一样访问计数:

    >> counts[2]
    3 
    

    【讨论】:

      【解决方案3】:
      lst = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'],['apple', 'apple']]
      def countList(lst): 
          lst_dict = {}
          for i in lst:
              if len(i) not in lst_dict:
                  lst_dict[len(i)] = 1
              else:
                  lst_dict[len(i)] = lst_dict[len(i)]+1
          return lst_dict
      
      print(countList(lst))
      
      >> {3: 1, 2: 2, 6: 1}
      

      这里的键是列表的长度,值是列表的数量。

      【讨论】:

        【解决方案4】:
        lst = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'], ['mango', 'apple'], ['mango', 'mango']]
        
        ctr_dict = {}
        
        for i in lst:
            item_length = len(i)
            if str(item_length) in ctr_dict:
                ctr_dict[str(item_length)] = ctr_dict[str(item_length)] + 1
            else:
                ctr_dict[str(item_length)] = 1
        
        for k,v in ctr_dict.items():
            print(k," strings:", v, "lists")
        
        output: 
        3  strings: 1 lists
        2  strings: 3 lists
        6  strings: 1 lists
        

        【讨论】:

        • 很高兴为您提供帮助,欢迎来到 Stack Overflow!如果此答案或任何其他答案确实解决了您的问题,请将其标记为已接受。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多