我有一个 list l = [1,2,2,3,1,1,2,3,4,5,6]
我不想使用 l.count(element)
方法。我不想使用 for 循环或迭代器。
输出如 {1: 3, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1}
最佳答案
使用集合的计数器
:
>>> from collections import Counter
>>> l = [1,2,2,3,1,1,2,3,4,5,6]
>>> Counter(l)
Counter({1: 3, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1})
关于python - Python中列表所有元素的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883672/