有一回对我说道,“你读过书么?”
我略略点一点头。
他说,“读过书,……我便考你一考。茴香豆的茴字,怎样写的?”
我想,讨饭一样的人,也配考我么?便回过脸去,不再理会。
孔乙己等了许久,很恳切的说道,“不能写罢?……我教给你,记着!这些字应该记着。将来做掌柜的时候,写账要用。”
我暗想我和掌柜的等级还很远呢,而且我们掌柜也从不将茴香豆上账;又好笑,又不耐烦,懒懒的答他道,“谁要你教,不是草头底下一个来回的回字么?”
孔乙己显出极高兴的样子,将两个指头的长指甲敲着柜台,点头说,“对呀对呀!……回字有四样写法,你知道么?”
– 鲁迅 《孔乙己》
1. 遍历统计
aaa = [1,2,2,2,2,3,3,4,0,5]
stat_dict={}
for i in aaa:
if i in stat_dict:
stat_dict[i] += 1
else:
stat_dict[i] = 1
2. 字典推导式
aaa = [1,2,2,2,2,3,3,4,0,5]
stat_dict = {k:aaa.count(k) for k in set(aaa)}
3. dict生成器
aaa = [1,2,2,2,2,3,3,4,0,5]
stat_dict = dict([ [k,aaa.count(k)] for k in set(aaa)])
4. counter
from collections import Counter
aaa = [1,2,2,2,2,3,3,4,0,5]
stat_dict=Counter(aaa)
5. pd.groupby
aaa = [1,2,2,2,2,3,3,4,0,5]
stat_dict = dict([ [k,aaa.count(k)] for k in set(aaa)])