max*_*max 7 python collections dictionary python-3.x
[Python 3.1]
我想要defaultdict(int),除了我想要的默认值1而不是0.
mou*_*uad 15
>>> def f():
return 1
>>> a = defaultdict(f)
>>> a[1]
1
Run Code Online (Sandbox Code Playgroud)
这是使用lambda表达式的另一个实现(来自kindall):
>>> a = defaultdict(lambda: 1)
Run Code Online (Sandbox Code Playgroud)