Home > OS >  How do i access a dictionary after using loops to convert some lists to dictionary? The code is belo
How do i access a dictionary after using loops to convert some lists to dictionary? The code is belo

Time:02-21

INSTRUCTION:

The data is organized such that the data at each index, from 0 to 33, corresponds to the same hurricane.

For example, names[0] yield the “Cuba I” hurricane, which occurred in months[0] (October) years[0] (1924).

Write a function that constructs a dictionary made out of the lists, where the keys of the dictionary are the names of the hurricanes, and the values are dictionaries themselves containing a key for each piece of data (Name, Month, Year, Max Sustained Wind, Areas Affected, Damage, Death) about the hurricane.

Thus the key "Cuba I" would have the value: {'Name': 'Cuba I', 'Month': 'October', 'Year': 1924, 'Max Sustained Wind': 165, 'Areas Affected': ['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], 'Damage': 'Damages not recorded', 'Deaths': 90}.

THESE ARE THE LISTS:

names = ['Cuba I', 'San Felipe II Okeechobee', 'Cuba II']
months = ['October', 'September', 'September']
years = [1924, 1928, 1932]
max_sustained_winds = [165, 160, 160]
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States']]
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M']
deaths = [90,4000,16]

THE CODE I HAVE TRIED:

hurricane_records = list(zip(names, months, years, max_sustained_winds, areas_affected, update_damages(), deaths))

dict1 = {}

strongest_hurricane_records = {}

for i in range(len(names)):
     dict1["Name"] = hurricane_records[i][0]
     dict1["Month"] = hurricane_records[i][1]
     dict1["Year"] = hurricane_records[i][2]
     dict1["Max Sustained Wind"] = hurricane_records[i][3]
     dict1["Areas Affected"] = hurricane_records[i][4]
     dict1["Damage"] = hurricane_records[i][5]
     dict1["Deaths"] = hurricane_records[i][6]

     strongest_hurricane_records[names[i]] = dict1

print(strongest_hurricane_records["Cuba I"])

My problem here is that when I tried to access the dictionary "Cuba I", instead of printing the values of "Cuba I" dictionary, it is printing the last dictionary which is this: {'Cuba II', 'September', 1928, 160, ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], '100M', 4000}

CodePudding user response:

You can make a list of all the values then use a for loop and then convert each value into dictionary using dict(value) and then append these results into another dictionary using.update() Features = [all features] Dict = {} for f in Features: Dict.update(dict(f)) else: print(Dict)

CodePudding user response:

The problem is that you have dict1 = {} outside of the for loop, so only one dictionary will be created, and each iteration of loop will modify the same dictionary. Simply move dict1 = {} into the loop to re-initialize it for each hurricane:

strongest_hurricane_records = {}

for i in range(len(names)):
     dict1 = {}  # Now dict1 is unique for each loop iteration
     dict1["Name"] = ...
     strongest_hurricane_records[names[i]] = dict1

CodePudding user response:

update_damages() isn't reproducible so I made my own lists which are similar to yours. Remember to run your code and fix errors before posting the minimal working example.

If I understand correctly you want to create a nested dictionary, i.e. a dictionary which contains other dictionaries. I give a simplified version. You can find more advanced in other very relevant questions like in How to zip three lists into a nested dict.

# Outer dictionary:
genders = ['male', 'female']

# Inner dictionary:
keys = ['name', 'surname', 'age']
names = ['Bob', 'Natalie']
surnames = ['Blacksmith', 'Smith']
ages = [10, 20]

inner_dictionaries = [dict(zip(keys, [names[i], 
                                      surnames[i], 
                                      ages[i]])) 
                      for i, elem in enumerate(names)]
# [{'name': 'Bob', 'surname': 'Blacksmith', 'age': 10}, 
# {'name': 'Natalie', 'surname': 'Smith', 'age': 20}]

outer_dictionaries = dict((keys, values) 
                           for keys, values in 
                           zip(genders, inner_dictionaries))
# {'male': {'name': 'Bob', 'surname': 'Blacksmith', 'age': 10}, 
# 'female': {'name': 'Natalie', 'surname': 'Smith', 'age': 20}}

By the way avoid range(len()) in a for loop if you want to loop like a native (also from R. Hettinger). Moving on, the inner_dictionaries comes from this for loop:

for index, element in enumerate(names):
    print(dict(zip(keys, [names[index], surnames[index], ages[index]])))
  • Related