简体   繁体   中英

creating sum of the values in nested Dictionary

My dictionary looks like the below:

my_single_dictionary={
    "totals": {"total": 26, "correct": 12, "false": 14, "correct_Percent": 46.15384615384615, "wrong_percent": 53.84615384615385},
    "label": {"ADDRESS": {"total": 11, "correct": 2, "false": 9, "correct_Percent": 18.181818181818183, "wrong_percent": 81.81818181818183}, 
              "NAME": {"total": 9, "correct": 6, "false": 3, "correct_Percent": 66.66666666666666, "wrong_percent": 33.33333333333333}, 
              "DATE_TIME": {"total": 6, "correct": 4, "false": 2, "correct_Percent": 66.66666666666666, "wrong_percent": 33.33333333333333}}
}

Here is pseudo-code?

grand_sum_percentages={}
for new_dictiony in list_of_10_dictionaies:
    similar_to_my_single_dictiony
# updating the percentage_correct and percentage_wrong based on the sum of wrong and correct of two dictionaries 
    for values in similar_to_my_single_dictiony.values():
        values+my_single_dictionary.values()
    some way to calculate the grand sum of `total`,               `correct`,`false`

What do I want to do?

I have a list of dictionaries, a single dictionary looks like my_single_dictionary I have percentage correct and percentage wrong for total and label {name, email, phone} wise. Now in the loop, I want to get a grand sum of percentage for all the dictionaries not a single one.

My final dictionary should look like this:

{'totals': {'total': 2000,
  'correct': 200,
  'false': 1800,
  'correct_Percent': xx.xx,
  'wrong_percent': xx.xx},
 'label': {'ADDRESS': {'total': 300,
   'correct': 150,
   'false': 150,
   'correct_Percent': xx,
   'wrong_percent': xx},
  'NAME': {'total': 600,
   'correct': 200,
   'false': 400,
   'correct_Percent': xxx,
   'wrong_percent': xxx}}}

This is an example of how you can iterate over the keys to sum the values within. This works for the "totals" key, you should figure out how to do the "label" yourself.

dictionary_list = [{
    "totals": {"total": 26, "correct": 12, "false": 14, "correct_Percent": 46.15384615384615, "wrong_percent": 53.84615384615385},
    "label": {"ADDRESS": {"total": 11, "correct": 2, "false": 9, "correct_Percent": 18.181818181818183, "wrong_percent": 81.81818181818183}, 
              "NAME": {"total": 9, "correct": 6, "false": 3, "correct_Percent": 66.66666666666666, "wrong_percent": 33.33333333333333}, 
              "DATE_TIME": {"total": 6, "correct": 4, "false": 2, "correct_Percent": 66.66666666666666, "wrong_percent": 33.33333333333333}}
}]

for i in range(9):
    dictionary_list.append(dictionary_list[0].copy())

# Relevant part of the code for you below this line 
final_dict = {"totals" : {}, "label" : {"ADDRESS": {}, "NAME": {}, "DATE_TIME": {}}}

for key in dictionary_list[0]["totals"]:
    for dictionary in dictionary_list:
        if key not in final_dict["totals"]:
            final_dict["totals"][key] = 0
        final_dict["totals"][key] += dictionary["totals"][key]

print(final_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM