defaultdict 的用法

 1 from collections import defaultdict
 2 
 3 a=[1,1,2,2,2,2,3,3,4,5,5,5,9,9,9]
 4 
 5 def foo(a):
 6     counts=defaultdict(int)
 7     for x in a:
 8         counts[x]+=1
 9         print(counts)
10     return counts
11 
12 foo(a)

 

posted @ 2017-08-20 17:10  人到中年万事休  阅读(127)  评论(0编辑  收藏  举报