问题
I have 2 example lists and what I want to achieve is to obtain a nested default dictionary with the sum of the values.
The following code works nice:
from collections import defaultdict
l1 = [1,2,3,4]
l2 = [5,6,7,8]
dd = defaultdict(int)
for i in l1:
for ii in l2:
dd[i] += ii
but what I'm trying to do is to create a default key in the d
dictionary:
from collections import defaultdict
l1 = [1,2,3,4]
l2 = [5,6,7,8]
dd = defaultdict(int)
for i in l1:
for ii in l2:
dd[i]['mykey'] += ii
and this throws an error:
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 12, in <module>
TypeError: 'int' object is not subscriptable
Basically what I'm not able to understand is if there is the chance to mix defaultdict(dict)
and defaultdict(int)
.
回答1:
The defaultdict data structure receives a function that will supply the default value, so if you want to create a defautdict(int)
as default value provide a function that does that, for example lambda : defaultdict(int)
:
from collections import defaultdict
from pprint import pprint
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
dd = defaultdict(lambda : defaultdict(int))
for i in l1:
for ii in l2:
dd[i]['mykey'] += ii
pprint(dd)
Output
defaultdict(<function <lambda> at 0x7efc74d78f28>,
{1: defaultdict(<class 'int'>, {'mykey': 26}),
2: defaultdict(<class 'int'>, {'mykey': 26}),
3: defaultdict(<class 'int'>, {'mykey': 26}),
4: defaultdict(<class 'int'>, {'mykey': 26})})
回答2:
you want a default dict of a default dict of integer:
dd = defaultdict(lambda: defaultdict(int))
after running your code:
>>> dd
{1: defaultdict(<class 'int'>, {'mykey': 26}),
2: defaultdict(<class 'int'>, {'mykey': 26}),
3: defaultdict(<class 'int'>, {'mykey': 26}),
4: defaultdict(<class 'int'>, {'mykey': 26})}
来源:https://stackoverflow.com/questions/53894698/mixing-defaultdict-dict-and-int