defaultdict
接受不带参数的可调用函数:
from collections import defaultdict
words = ['hello', 'world', 'nice', 'world']
#使用lambda来定义简单的函数
counter = defaultdict(lambda: 0)
for kw in words:
counter[kw] += 1
defaultdict
接受一个类型作为参数
from collections import defaultdict
dd = defaultdict(list)
defaultdict(<type 'list'>, {})
#给它赋值,同时也是读取
dd['h'h]
defaultdict(<type 'list'>, {'hh': []})
dd['hh'].append('haha')
defaultdict(<type 'list'>, {'hh': ['haha']})