from collections import Counter
c_num = Counter('Hello world') # 统计出现的每个字符数量
print(c_num)
for key, value in c_num.items():
print('%s: %s个' % (key, value))
print(c_num.most_common(2)) # 随机获取2个元素并且返回格式为:[('1',3),(' ',1)]
print(c_num.most_common(4))
#
# Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
# H: 1个
# e: 1个
# l: 3个
# o: 2个
# : 1个
# w: 1个
# r: 1个
# d: 1个
# [('l', 3), ('o', 2)]
# [('l', 3), ('o', 2), ('H', 1), ('e', 1)]