Reputation: 4354
I have a CSV file that looks something like this: It shows electricity consumption of 4 countries from 1980 to 2014. I'm trying to create a nested dictionary where for example
consumption['United States'][1980]
will return the correct value. I have an array with the list of integer years and I'm trying to create the dictionary like this:
file = open('power dataset.csv', 'r')
years = list(range(1980, 2015))
consumption = {}
generation = {}
generation = False
for line in file:
if("Nuclear" in line):
break
split = line.split(",")
if split[0] == "Generation":
generation = True
if "Egypt" == split[0] or split[0] == "Germany" or split[0] == "Netherlands" or split[0] == "United States":
values = split[2:]
if not generation:
i = 0
for year in years:
country = split[0]
consumption[country] = {year: values[i]}
i = i+1
where values is an array containing the values of the corresponding year. The problem I'm having is that the dictionary ends up only containing one year and one value (which are the last ones). So if I try printing something like consumption['United States'][1980]
I get an error because there's no entry for 1980 in the dictionary, only 2014.
I feel like I'm missing something fairly simple but I can't quite put my finger on it.
Here is the entire CSV file.
Upvotes: 2
Views: 2363
Reputation: 82929
The problem seems to be that with this:
for year in years:
consumption[country] = {year: values[i]}
you overwrite the previous value for consumption[country]
in each iteration of the loop.
Instead, try this:
if country in ("Egypt", "Germany", "Netherlands", "United States"):
if not generation:
consumption[country] = {year: vals for year, vals in zip(years, values)}
Step-by-step breakdown example of the dictionary-comprehension with zip
:
>>> years = [1980, 1981, 1982, 1983]
>>> values = [1, 2, 3, 4]
>>> zip(years, values)
[(1980, 1), (1981, 2), (1982, 3), (1983, 4)]
>>> {year: vals for year, vals in zip(years, values)}
{1980: 1, 1981: 2, 1982: 3, 1983: 4}
Alternatively, you could initialize consumption[country]
as consumption[country] = {}
before the inner loop and then use consumption[country][year] = values[i]
as in your original code before the edit.
Upvotes: 3