是否可以更快地计算列表中数字出现的数量?

发布于 2025-02-03 08:40:19 字数 420 浏览 2 评论 0原文

我正在尝试编写一个函数来计算列表中数字的出现,并且该顺序是根据数字上升(从列表中的0到最大值)而不是出现。这是我写的功能:

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

例如:

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

我了解到sample.count如果列表中有更多数字,则可以缓慢工作。我可以编写此功能更快/更简单吗?

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. 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. Is there a faster/simpler way I can write this function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃不饱 2025-02-10 08:40:19

收集模块的计数器是计算列表中项目出现数量的好方法

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]

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]
新人笑 2025-02-10 08:40:19

如果您不想使用计数器,则可以在创建适当大小的结果数组之后简单地迭代列表中的值:

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

输出:

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

示例数据的速度明智,如果counter 解决方案是1x时间,这是关于4X和您现有的解决方案,涉及8x。随着列表的越来越长,Counter的速度优势降低,相对性能更像1X2X4X代码>。

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. As the lists get longer, the speed advantage of Counter decreases and the relative performance is more like 1x, 2x and 4x.

初懵 2025-02-10 08:40:19

如果您不想通过样本的元素使用计数并更新结果。它可以减少找到元素计数所需的多次遍历所需的时间。

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

If you don't want to use count iterate through the elements of the samples and update the result. 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文