Python中使用collections.Counter统计序列中元素的频率

from collections import Counter
import numpy as np

print("\n列表元素统计:")
ls = [1, 1, 1, 2, 2, 2, 3.3, 3.3, "aa", "aa", "bb"]
r = Counter(ls)  # Counter类型
print(dict(r))  # 转化为字典

print("\n数组元素统计:")
arry = np.array([1, 1, 1, 2, 2, 2, 3.3, 3.3, "aa", "aa", "bb"])
r = Counter(arry)
print(dict(r))

print("\n字符串元素统计:")
str = "11122233aabb"
r = Counter(str)
print(dict(r))

Python中使用collections.Counter统计序列中元素的频率_第1张图片

你可能感兴趣的:(python,python,开发语言,collections,Counter,频数统计)