使用Counter方法
from collections import Counter
list1 = ["a", "a", "b", "a", "b", "c"]
print(Counter(list1)) # 打印Counter({'a': 3, 'b': 2, 'c': 1})
# 可以转成字典类型
print(dict(Counter(list1))) # 打印{'a': 3, 'b': 2, 'c': 1}
使用Counter方法
from collections import Counter
list1 = ["a", "a", "b", "a", "b", "c"]
print(Counter(list1)) # 打印Counter({'a': 3, 'b': 2, 'c': 1})
# 可以转成字典类型
print(dict(Counter(list1))) # 打印{'a': 3, 'b': 2, 'c': 1}