简体   繁体   English

有没有更快的方法可以计算列表中某个数字的出现次数?

[英]Is there a faster way I can count the number of occurrences of a number in a list?

I am trying to write a function to count the occurrences of a number in a list, and the order is ascending according to the number (from 0 to the maximum value in the list), not the occurrences.我正在尝试编写一个函数来计算列表中某个数字的出现次数,并且顺序根据数字升序(从 0 到列表中的最大值),而不是出现次数。 Here's the function I wrote:这是我写的函数:

def sort_counts(sample):
    result = []
    for i in range(max(sample)+1):
        result.append(sample.count(i))
    return result

For example:例如:

>>> sort_counts([1,2,2,3,3,4,1,1,1,1,2,5])
>>> [0, 5, 3, 2, 1, 1]

I learned that sample.count would work slowly if there are more numbers in the list.我了解到,如果列表中有更多数字, sample.count会运行缓慢。 Is there a faster/simpler way I can write this function?有没有更快/更简单的方法可以编写这个函数?

Counter from collections module is a nice way to count the number of occurrences of items in a list集合模块中的计数器是一种计算列表中项目出现次数的好方法

from collections import Counter
lst = [1,2,2,3,3,4,1,1,1,1,2,5]
# create a counter object
c = Counter(lst)
# get the counts
[c[i] for i in range(max(c)+1)]
# [0, 5, 3, 2, 1, 1]

If you don't want to use a counter, then you can simply iterate over the values in the list after creating a result array of the appropriate size beforehand:如果您不想使用计数器,则可以在事先创建适当大小的结果数组后简单地迭代列表中的值:

sample = [1,2,2,3,3,4,1,1,1,1,2,5]
result = [0] * (max(sample)+1)
for v in sample:
    result[v] += 1

Output:输出:

[0, 5, 3, 2, 1, 1]

Speed wise for your sample data, if the Counter solution is 1x time, this is about 4x and your existing solution about 8x .对于您的样本数据,速度明智,如果Counter解决方案是1x时间,这大约是4x而您现有的解决方案大约是8x As the lists get longer, the speed advantage of Counter decreases and the relative performance is more like 1x , 2x and 4x .随着列表变长, Counter的速度优势降低,相对性能更像1x2x4x

If you don't want to use count iterate through the elements of the samples and update the result.如果您不想使用 count 遍历样本的元素并更新结果。 It can reduce the time required for multiple traversal required to find the count of the element.它可以减少查找元素计数所需的多次遍历所需的时间。

def sort_counts(sample):
    result = [0]*(max(sample)+1)
    for s in samples:
        result[s]+=1
    return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM