【发布时间】:2021-08-27 14:37:24
【问题描述】:
我想获取给定list 中每个值的频率。
fruits = ["apple", "banana", "cherry", "apple", "banana"]
counts = [fruits.count(x) for x in fruits]
print(counts)
>>> [2, 2, 1, 2, 2] # "apple", "banana", "cherry", "apple", "banana"
期望的输出
>>> [2, 2, 1] # "apple", "banana", "cherry"
其次;执行此操作的计算效率最高的方式可能是什么?
【问题讨论】:
-
抱歉标题的措辞。我可能需要尽快改写它
-
试试看
Counter -
哦,好的,我会继续尝试有效的解决方案。然而,我有兴趣在相当大的范围内这样做。因此希望使用 primitive Python 代码来加快速度。
标签: python python-3.x list