[英]Is there array_count_values() analogue for Python 3.x or best way to do this?
Is there array_count_values() analogue or fastest way to do this in Python 3.x在 Python 3.x 中是否有 array_count_values() 模拟或最快的方法来做到这一点
from从
d = ["1", "this", "1", "is", "Sparta", "Sparta"]
to到
{
'1': 2,
'this': 1,
'is': 1,
'Sparta': 2
}
You can count the occurrence of each element in a list using Counter
:您可以使用Counter
列表中每个元素的出现Counter
:
from collections import Counter
l = [1, "this", 1, "is", "Sparta", "Sparta"]
print(Counter(l))
This prints这打印
Counter({1: 2, 'Sparta': 2, 'this': 1, 'is': 1})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.