collections.defaultdict()

https://www.cnblogs.com/herbert/archive/2013/01/09/2852843.html

>>> import collections
>>> d = collections.defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>>
>>> d
defaultdict(<type 'int'>, {'i': 4, 'p': 2, 's': 4, 'm': 1})

猜你喜欢

转载自www.cnblogs.com/pengwang52/p/11861129.html