Home > Back-end >  sum in list of dictionaries python with exception
sum in list of dictionaries python with exception

Time:11-24

how do I get the sum of money and spent from a list of dictionaries where

sum of money = (sum of money of shirt color blue and red) and (sum of money of shirt color yellow and green)

sum of spent = (sum of spent of shirt color blue and red) and (sum of spent of shirt color yellow and green)

should i make new dictionary for shirtcolor blue and red and another one for yellow and green?

people = [{'name': 'A', 'shirtcolor':'blue', 'money':'100', spent:'50'}, {'name': 'B', 'shirtcolor':'red', 'money':'70', spent:'50'}, {'name': 'C', 'shirtcolor':'yellow', 'money':'100', spent:'70'}, {'name': 'D', 'shirtcolor':'blue', 'money':'200', spent:'110'},{'name': 'E', 'shirtcolor':'red', 'money':'130', spent:'50'}, {'name': 'F', 'shirtcolor':'yellow', 'money':'200', spent:'70'},{'name': 'G', 'shirtcolor':'green', 'money':'100', spent:'50'}]

expected output:

Total Money:  500 and 400

Total spent: 260 and 190 

CodePudding user response:

The data is

people = [{'name': 'A', 'shirtcolor': 'blue', 'money': '100', 'spent': '50'},
          {'name': 'B', 'shirtcolor': 'red', 'money': '70', 'spent': '50'},
          {'name': 'C', 'shirtcolor': 'yellow', 'money': '100', 'spent': '70'},
          {'name': 'D', 'shirtcolor': 'blue', 'money': '200', 'spent': '110'},
          {'name': 'E', 'shirtcolor': 'red', 'money': '130', 'spent': '50'},
          {'name': 'F', 'shirtcolor': 'yellow', 'money': '200', 'spent': '70'},
          {'name': 'G', 'shirtcolor': 'green', 'money': '100', 'spent': '50'}]

You need only one dictionary where the color is the key and the value is a dictionary with the keys "money" and "spent". Then you can add up all entries there.

color_sum = dict()
for entry in people:
    if entry['shirtcolor'] not in color_sum:
        color_sum[entry['shirtcolor']] = {'money':0, 'spent':0}
    color_sum[entry['shirtcolor']]['money']  = int(entry['money'])
    color_sum[entry['shirtcolor']]['spent']  = int(entry['spent'])

Using a defaultdict does make this easier.

from collections import defaultdict

color_sum = defaultdict(lambda: {'money':0, 'spent':0})
for entry in people:
    color_sum[entry['shirtcolor']]['money']  = int(entry['money'])
    color_sum[entry['shirtcolor']]['spent']  = int(entry['spent'])

The resulting data in color_sum will be this:

{'blue': {'money': 300, 'spent': 160}, 
 'red': {'money': 200, 'spent': 100}, 
 'yellow': {'money': 300, 'spent': 140}, 
 'green': {'money': 100, 'spent': 50}}

Now you can get the information you need.

money_red_blue = color_sum["red"]["money"]   color_sum["blue"]["money"]
money_yellow_green = color_sum["yellow"]["money"]  color_sum["green"]["money"]
print(f'Total money: {money_red_blue} and {money_yellow_green}')

This will output Total money: 500 and 400


In the comment was the question how to get all the money from shirts that don't have one of the colors green and yellow. In this case we will have to loop over the aggregated data in the dictionary and exclude the items with the keys "green" and "yellow".

money = 0
for k, v in color_sum.items():
    if k not in {'green', 'yellow'}:
        money  = v['money']
print(money)

Or as a one-liner with sum and a generator:

money = sum(v['money'] for k, v in color_sum.items() if k not in {'green', 'yellow'})
print(money)

CodePudding user response:

First you need to check the spent attribute. It should have this syntax: 'spent': 40. So people will be like this:

people = [{'name': 'A', 'shirtcolor':'blue', 'money':'100', 'spent':'50'}, {'name': 'B', 'shirtcolor':'red', 'money':'70', 'spent':'50'}, {'name': 'C', 'shirtcolor':'yellow', 'money':'100', 'spent':'70'}, {'name': 'D', 'shirtcolor':'blue', 'money':'200', 'spent':'110'},{'name': 'E', 'shirtcolor':'red', 'money':'130', 'spent':'50'}, {'name': 'F', 'shirtcolor':'yellow', 'money':'200', 'spent':'70'},{'name': 'G', 'shirtcolor':'green', 'money':'100', 'spent':'50'}]

Then you need to loop over the list and get all the values. You can do this by extending this code:

money_blue = 0
for i in range(len(people)):
    if people[i]['shirtcolor'] == "blue":
        money  = int(people[i]['money'])
    

print(money_blue)

CodePudding user response:

You can try like below:

product_list=[{"name": "A", "shirtcolor":"blue", "money":"100", "spent":"50"}, {"name": "B", "shirtcolor":"red", "money":"70", "spent":"50"}, {"name": "C", "shirtcolor":"yellow", "money":"100", "spent":"70"}, {"name": "D", "shirtcolor":"blue", "money":"200", "spent":"110"},{"name": "E", "shirtcolor":"red", "money":"130", "spent":"50"}, {"name": "F", "shirtcolor":"yellow", "money":"200", "spent":"70"},{"name": "G", "shirtcolor":"green", "money":"100", "spent":"50"}]; print(product_list);

#sum of spent for blue and red blueSpent = sum([int(x["spent"]) for x in product_list if x["shirtcolor"]=="blue" or x["shirtcolor"]=="red"]) print(blueSpent);

#sum of spent for green and yellow greenSpent = sum([int(x["spent"]) for x in product_list if x["shirtcolor"]=="green" or x["shirtcolor"]=="yellow"]) print(greenSpent);

#sum of spent for blue and red blueMoney = sum([int(x["money"]) for x in product_list if x["shirtcolor"]=="blue" or x["shirtcolor"]=="red"]) print(blueMoney);

#sum of money for green and yellow greenMoney = sum([int(x["money"]) for x in product_list if x["shirtcolor"]=="green" or x["shirtcolor"]=="yellow"]) print(greenMoney);

  • Related