This question already has an answer here:
- Iterate over nested dictionary 5 answers
I have a nested dictionary
dict_features = {'agitacia/6.txt': {'samoprezentacia': 0, 'oskorblenie': 1},
'agitacia/21.txt': {'samoprezentacia': 0, 'oskorblenie': 0}}
I'm trying to output a new dictionary features_agit_sum
which consists of a key from a previous dictionary and a sum of values of a "deeper" dictionary. So I need to sum 0+1 that is int type. The output should be:
{'agitacia/6.txt': 1, 'agitacia/21.txt': 0}
Below are several attempts with different errors; don't how to iterate correctly:
features_agit_sum = {}
def vector_agit_sum(dict_features):
for key, value in dict_features:
features_agit_sum[key] = sum(dict_features.items()[key])
print (features_agit_sum)
return features_agit_sum
ValueError: too many values to unpack (expected 2) dict_features.items()[key] - try to access deeper dict
features_agit_sum = {}
def vector_agit_sum(dict_features):
for key in dict_features:
for item, value in dict_features.items():
features_agit_sum[key] = sum([item])
print (features_agit_sum)
return features_agit_sum
TypeError: unsupported operand type(s) for +: 'int' and 'str' - Why, it's integers!
features_agit_sum = {}
def vector_agit_sum(dict_features):
files = dict_features.keys()
for key, value in dict_features.items():
features_agit_sum[files] = sum(dict_features.items()[key])
print (features_agit_sum)
return features_agit_sum
TypeError: 'dict_items' object is not subscriptable
the shortest (and fastest) way may be as this:
Try this, using a dictionary comprehension (this will work in both Python 2.7+ and 3.x):
If using Python 2.7+, the recommended way is to explicitly use iterators, in Python 3.x the previous snippet already uses them:
Either way, it returns the expected result:
Use a dict comprehension:
If you are using Python 3, remove the
iter
prefixes, so use.values()
and.items()
.Demo:
Use
isinstance
to check the type and take the correct action. If it is aint
, add it to the running total. Otherwise, recurse to get the total sum contained within that dictionary.