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)
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)