I'm having an issue updating a list in a nested defaultdict.
Here is my code:
a = ['20160115', 'shadyside medical building', 1, 'Review']
b = ['20160115', 'shadyside medical building', 1, 'Video']
c = ['20160215', 'shadyside medical building', 1, 'Video']
d = ['20160215', 'medical building', 1, 'Video']
f = [a,b,c,d]
nested_dict = defaultdict(dict)
for date,keyword,pos,feature in f:
nested_dict[keyword].update({feature : [pos]})
nested_dict[keyword].update({feature : [pos]})
Here is the output:
{'shadyside medical building':
{'Review': [1],
'Video': [1]},
'medical building':
{'Video': [1]}}
The desired output is:
{'shadyside medical building':
{'Review': [1],
'Video': [1,1]},
'medical building':
{'Video': [1]}}
Notice the second item for video was added to the video list.
You didn’t nest any defaultdicts, so do that:
nested_dict = defaultdict(lambda: defaultdict(list))
and
nested_dict[keyword][feature].append(pos)
You can also create an infinitely-nesting defaultdict:
NDict = lambda: None
NDict = lambda: defaultdict(NDict)
ouroboros = NDict()
ouroboros[1][2][3][4][5][6][7][8][9] = True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With