To improve the display of my data, I would like to convert a nested list to a dictionary list. Here is an excerpt from my nested list:
[['Ouzbekistan', 2046.4, 'Portugal'],
['Ouzbekistan', 830.4, 'IlesCanaries'],
['Paraguay', 830.4, 'IlesCanaries'],
['BurkinaFaso', 1084.6, 'Bahamas']]
As you can see we have many times the first argument in the list coming back (example: Ouzbekistan).
In order to access my data more quickly later, I considered a dictionary list having as key: "country",amount and [country_take]. Here is a return with the above example
[{country:'Ouzbekustan',amount:2876.8,country_take:['Portugal','IlesCanaries']},
{country:'Paraguay',amount:830.4,Country_take:['IlesCanaries']},
{country:'BurkinaFaso',amount:1084.6,Country_take:['Bahamas']}]
The main idea is to have "country" as the key which would allow me to add the amounts and combine the countries that were counted.
Do you have any idea how I can loop through my nested list to retrieve the information to make a dictionary list out of it ?
Thank you in advance for your help !
CodePudding user response:
Group your data and aggregate your values
import operator
import itertools
source = [['Ouzbekistan', 2046.4, 'Portugal'],
['Ouzbekistan', 830.4, 'IlesCanaries'],
['Paraguay', 830.4, 'IlesCanaries'],
['BurkinaFaso', 1084.6, 'Bahamas']]
out = []
for country, content in itertools.groupby(sorted(source, key=operator.itemgetter(0)), key=operator.itemgetter(0)):
data = list(content)
out.append({"country": country,
"amount": sum([x[1] for x in data]),
"country_take": [x[2] for x in data]})
print(out)
[{'country': 'BurkinaFaso', 'amount': 1084.6, 'country_take': ['Bahamas']},
{'country': 'Ouzbekistan', 'amount': 2876.8, 'country_take': ['Portugal', 'IlesCanaries']},
{'country': 'Paraguay', 'amount': 830.4, 'country_take': ['IlesCanaries']}]
But this won't help you access your data faster yet, it just makes the search more verbose. You probably want to use the country name as a dictionary key.
import operator
import itertools
source = [['Ouzbekistan', 2046.4, 'Portugal'],
['Ouzbekistan', 830.4, 'IlesCanaries'],
['Paraguay', 830.4, 'IlesCanaries'],
['BurkinaFaso', 1084.6, 'Bahamas']]
out = {}
for country, content in itertools.groupby(sorted(source, key=operator.itemgetter(0)), key=operator.itemgetter(0)):
data = list(content)
out[country] = {"amount": sum([x[1] for x in data]),
"country_take": [x[2] for x in data]}
print(out)
{'BurkinaFaso': {'amount': 1084.6, 'country_take': ['Bahamas']},
'Ouzbekistan': {'amount': 2876.8, 'country_take': ['Portugal', 'IlesCanaries']},
'Paraguay': {'amount': 830.4, 'country_take': ['IlesCanaries']}}
You can then easily find your entry with out[country]
CodePudding user response:
Following should work too,
data = [['Ouzbekistan', 2046.4, 'Portugal'],
['Ouzbekistan', 830.4, 'IlesCanaries'],
['Paraguay', 830.4, 'IlesCanaries'],
['BurkinaFaso', 1084.6, 'Bahamas']]
dct = {}
for row in data:
country,amount,country_take = row
if dct.get(country,False):
dct[country]["amount"] =amount
dct[country]["country_take"].append(country_take)
continue
dct[country] = {
"country":country,
"amount":amount,
"country_take":[country_take]
}
output = [ val for val in dct.values()]