gpt4 book ai didi

python - 是否有适用于 Python 3.x 的 array_count_values() 类似物或最佳方法?

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:03 24 4
gpt4 key购买 nike

在 Python 3.x 中是否有 array_count_values() 模拟或最快的方法

来自

d = ["1", "this", "1", "is", "Sparta", "Sparta"]

{
'1': 2,
'this': 1,
'is': 1,
'Sparta': 2
}

最佳答案

您可以使用 Counter 计算列表中每个元素的出现次数:

from collections import Counter

l = [1, "this", 1, "is", "Sparta", "Sparta"]
print(Counter(l))

这打印

Counter({1: 2, 'Sparta': 2, 'this': 1, 'is': 1})

repl.it link

关于python - 是否有适用于 Python 3.x 的 array_count_values() 类似物或最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39883472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com