[英]Is there array_count_values() analogue for Python 3.x or best way to do this?
在 Python 3.x 中是否有 array_count_values() 模擬或最快的方法來做到這一點
從
d = ["1", "this", "1", "is", "Sparta", "Sparta"]
到
{
'1': 2,
'this': 1,
'is': 1,
'Sparta': 2
}
您可以使用Counter
列表中每個元素的出現Counter
:
from collections import Counter
l = [1, "this", 1, "is", "Sparta", "Sparta"]
print(Counter(l))
這打印
Counter({1: 2, 'Sparta': 2, 'this': 1, 'is': 1})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.