简体   繁体   English

是否有适用于 Python 3.x 的 array_count_values() 模拟或执行此操作的最佳方法?

[英]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})

repl.it link复制链接

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

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