每天一个python小技巧

HuahuatiiLess than 1 minutepython数据处理python

每天一个python小技巧

1 统计不同元素在列表中出现的次数(Counter)

Details
from collections import Counter
fruit = ['apple', 'banana', 'orange', 'pear', 'apple', 'orange', 'banana', 'apple', 'pear', 'orange', 'fig']
print(Counter(fruit))
# ------------------------- RESULT -------------------------
Counter({'apple': 3, 'orange': 3, 'banana': 2, 'pear': 2, 'fig': 1})