The constructor defaultdictexpects the called. defaultdict(int)is the default dictionary, not called. Using lambda, it can work, however:
c = collections.defaultdict(lambda: collections.defaultdict(int))
This works because what I pass to the external defaultdictis the callable, which creates a new one defaultdictwhen called.
Here is an example:
>>> import collections
>>> c = collections.defaultdict(lambda: collections.defaultdict(int))
>>> c[5][6] += 1
>>> c[5][6]
1
>>> c[0][0]
0
>>>
source
share