I'm quite new to Python and just started working with dictionaries. I have the following question:
Given the following dictionary:
{'Berlin': {'Country': 'Germany', 'Population': 3.502},
'New York': {'Country': 'USA', 'Population': 8.406},
'Munich': {'Country': 'Germany', 'Population': 1.388}, ... }
How could I sum the population over countries? Thank you!
Avihoo Mamka
4,5163 gold badges30 silver badges42 bronze badges
asked Dec 13, 2016 at 10:06
Patrick BaladaPatrick Balada
1,2701 gold badge18 silver badges35 bronze badges
You can use
itertools.groupby[]
to group your data by country like below:
from itertools import groupby
my_dict = {'Berlin': {'Country': 'Germany', 'Population': 3.502}, 'New York': {'Country': 'USA', 'Population': 8.406}, 'Munich': {'Country': 'Germany', 'Population': 1.388}}
population = {}
for k,v in groupby[sorted[my_dict.items[]], key=lambda i:i[1]['Country']]:
population[k] = sum[item[1]['Population'] for item in list[v]]
Output:
>>> population
{'Germany': 4.89, 'USA': 8.406}
answered Dec 13, 2016 at 10:25
country_to_population = {}
for city, data in dict.iteritems[]:
country = data['Country']
if country not in country_to_population:
country_to_population[country] = 0
country_to_population[country] += data['Population']
You iterate the main dictionary and add population to a new dictionary that maps a country to population.
If using Python 3, do items[]
instead of iteritems[]
.
answered Dec 13, 2016 at 10:09
Rok PovsicRok Povsic
4,4065 gold badges36 silver badges48 bronze badges
1
You can go inside the dict iterating on the key:
dict = {'Berlin': {'Country': 'Germany', 'Population': 3502},
'New York': {'Country': 'USA', 'Population': 8406},
'Munich': {'Country': 'Germany', 'Population': 1388}}
country = []
sum = {}
for key in dict:
coun = dict[key]['Country']
if coun in country:
pass
else:
country.append[dict[key]['Country']]
sum[coun] = 0
sum[coun] += dict[key]['Population']
for key in sum:
print[key, sum[key]]
answered Dec 13, 2016 at 10:10
Marco smdmMarco smdm
9921 gold badge13 silver badges24 bronze badges
You can use defaultdict
from collections module, with int
assigned to it in order to have a default value of 0
to each country you are about to sum.
The code looks like:
from collections import defaultdict
country_dict = {"Berlin":{"Country":"Germany","Population":3.502},"Munich":{"Country":"Germany","Population":1.388},"New York":{"Country":"USA","Population":8.406}}
sum_countries_dict = defaultdict[int]
for item in country_dict.values[]:
sum_countries_dict[item['Country']] += item['Population']
And the result is:
>>> country_dict
{'Berlin': {'Country': 'Germany', 'Population': 3.502}, 'New York': {'Country': 'USA', 'Population': 8.406}, 'Munich': {'Country': 'Germany', 'Population': 1.388}}
>>> sum_countries_dict
defaultdict[, {'Germany': 4.89, 'USA': 8.406}]
>>> sum_countries_dict['Germany']
4.89
>>> sum_countries_dict['USA']
8.406
answered Dec 13, 2016 at 10:13
Avihoo MamkaAvihoo Mamka
4,5163 gold badges30 silver badges42 bronze badges