簡體   English   中英

是否有適用於 Python 3.x 的 array_count_values() 模擬或執行此操作的最佳方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM