Codecademy Python Dictionaries Challenge Project: Explained Step By Step
So if you have never known Codecademy before, it is basically an online course provider who mainly teaches programming languages, computer science, web development, and data science.
From time to time, a student will have to do a project during the course. I am currently on the Data Science Track. I have finished some projects. I found them fun, interesting, and very assimilable.
I decided to pick one of their projects about Dictionary to write in this post. I will walk you through how I work around the questions by using for loop, list, dictionary, and other functions and methods used with them.
I would like to highlight that there are many ways to write code to get the answers. For the readers who are advanced in Python, you may come up with better and shorter lines of code than the ones I have here.
For those who are at the beginner stage of the programming journey in Data Science, this post will be very insightful on how you can use different functions to get the answers. I will be using comments within the codes to explain what I was doing. Let’s begin, shall we?
Project Overview
In this project, you start off with 7 lists. Some contain integers or string, while others contain both. One of them even contains another list inside it. You can see these lists below.
# names of hurricanes
names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II', 'CubaBrownsville', 'Tampico', 'Labor Day', 'New England', 'Carol', 'Janet', 'Carla', 'Hattie', 'Beulah', 'Camille', 'Edith', 'Anita', 'David', 'Allen', 'Gilbert', 'Hugo', 'Andrew', 'Mitch', 'Isabel', 'Ivan', 'Emily', 'Katrina', 'Rita', 'Wilma', 'Dean', 'Felix', 'Matthew', 'Irma', 'Maria', 'Michael']
# months of hurricanes
months = ['October', 'September', 'September', 'November', 'August', 'September', 'September', 'September', 'September', 'September', 'September', 'October', 'September', 'August', 'September', 'September', 'August', 'August', 'September', 'September', 'August', 'October', 'September', 'September', 'July', 'August', 'September', 'October', 'August', 'September', 'October', 'September', 'September', 'October']
# years of hurricanes
years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 2017, 2018]
# maximum sustained winds (mph) of hurricanes
max_sustained_winds = [165, 160, 160, 175, 160, 160, 185, 160, 160, 175, 175, 160, 160, 175, 160, 175, 175, 190, 185, 160, 175, 180, 165, 165, 160, 175, 180, 185, 175, 175, 165, 180, 175, 160]
# areas affected by each hurricane
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]
# damages (USD($)) of hurricanes
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B']
# deaths for each hurricane
deaths = [90,4000,16,3103,179,184,408,682,5,1023,43,319,688,259,37,11,2068,269,318,107,65,19325,51,124,17,1836,125,87,45,133,603,138,3057,74]
These data are about hurricanes, their names, month and year of occurrences, how strong the wind was, location affected, and their damages on properties and human lives. One important thing to keep in mind is how the dataset is organized. In each list, there are 34 elements, indexed from 0 to 33. Each index of each list is the information belonged to the hurricane on that same index.
For example, name[0] is hurricane, “Cube I”. It took place on month[0] and year [0] which are October 1924. The maximum wind speed of this hurricane was 165. It affected a list of area such as Central America, Mexico, Cuba, Florida, and Bahamas. This list of area is actually the index zero element of area_affected. I assume now you understand what I mean about how this data is organized.
Project Goal
Now the goal is to analyze this dataset. You will do so by writing functions which will give the answer to the questions you have for your analyzing purpose.
There are 9 questions the project is asking to find answers. The level of difficulty and complexity increases as we move along the questions. As you will see, the first couple questions are easy. After that, it requires some strategies to find the answer.
Here we go! Let’s start analyzing this hurricane dataset with our functions.
1. Write a function that returns a new list of updated damages where recorded data is converted to float values and the missing data stays the same as “Damages not recorded”
Solution:
This first one is the easiest one. If you take a look at damages list, you can see that M and B are used to refer to millions and billions. In other words, this list contains string, not number. The project asks us to convert these into float (data type for number), while keeping the missing data the same as “Damages not recorded.”
My strategy is to use for loop to go through each element of the list. Since the current stage of each element now is string, I will check each element for M and B to make change before appending into a new list which starts out as an empty list.
If it is M, I multiply the number before it with 1000000. If it is B, I will multiply the number before it with 1000000000. When it gets to the list element which is “Damages not recorded”, I will just append the whole strings to the new list.
Here is how my code looks like and how they return a new converted list.
# write your update damages function here:
def to_money(demage):
new_demage = []
for i in demage:
if i == "Damages not recorded":
new_demage.append(i)
elif "M" in i: #this is where I check for M
"""if it does, then I assign that string except M to variable a
then use float() to covert it as float before multiplication.
I do the same thing for B.
"""
a = i[:-1]
a = float(a)*1000000
new_demage.append(a)
elif "B" in i:
a = i[:-1]
a = float(a)*1000000000
new_demage.append(a)
return new_demage
new_damages = to_money(damages)
print(new_damages)
>>>[output]:
['Damages not recorded', 100000000.0, 'Damages not recorded', 40000000.0, 27900000.0, 5000000.0, 'Damages not recorded', 306000000.0, 2000000.0, 65800000.0, 326000000.0, 60300000.0, 208000000.0, 1420000000.0, 25400000.0, 'Damages not recorded', 1540000000.0, 1240000000.0, 7100000000.0, 10000000000.0, 26500000000.0, 6200000000.0, 5370000000.0, 23300000000.0, 1010000000.0, 125000000000.0, 12000000000.0, 29400000000.0, 1760000000.0, 720000000.0, 15100000000.0, 64800000000.0, 91600000000.0, 25100000000.0]
2. Write a function that creates a dictionary out of the 7 lists provided. This new dictionary will have the keys as the names of the hurricanes and the values as another dictionary whose keys are “Name, Month, Year, Max Sustained Wind, Areas Affected, Damage, Death” while values are the elements of each list.
For example, “Cuba I” will be a key of this outcome dictionary. Its value will be
{'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}
Solution:
This one is also easy. What you want to do is to loop through all the lists and each time you take each element from the lists and put them into a dictionary which has the structure as the example project shown. Let’s look at my codes below.
# write your construct hurricane dictionary function here:
def to_dictionary(name, month, year, max_sus_wind, areas, demages, death):
"""
I use zip() to combine all lists in the parameters of this functions and save it
to a variable called zipped. It is easier when I want to loop through these list later.
"""
zipped = zip(name, month, year, max_sus_wind, areas, demages, death)
"""
I create an empty dictionary which will later store the information
the way the project asked.
"""
new_dictionary = {}
for name, val_mon, val_year, val_max, val_area, val_demage, val_death in zipped:
new_dictionary[name] = {"Name":name,
"Month":val_mon,
"Year":val_year,
"Max Sustained Wind":val_max,
"Area Affected":val_area,
"Damage":val_demage,
"Deaths":val_death}
return new_dictionary
hurricane_records= to_dictionary(names, months, years, max_sustained_winds, areas_affected, new_damages, deaths)
print(hurricane_records)
>>>[output]:
{'Cuba I': {'Name': 'Cuba I', 'Month': 'October', 'Year': 1924, 'Max Sustained Wind': 165, 'Area Affected': ['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], 'Damage': 'Damages not recorded', 'Deaths': 90}, 'San Felipe II Okeechobee': {'Name': 'San Felipe II Okeechobee', 'Month': 'September', 'Year': 1928, 'Max Sustained Wind': 160, 'Area Affected': ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], 'Damage': 100000000.0, 'Deaths': 4000}, 'Bahamas': {'Name': 'Bahamas', 'Month': 'September', 'Year': 1932, 'Max Sustained Wind': 160, 'Area Affected': ['The Bahamas', 'Northeastern United States'], 'Damage': 'Damages not recorded', 'Deaths': 16}, 'Cuba II': {'Name': 'Cuba II', 'Month': 'November', 'Year': 1932, 'Max Sustained Wind': 175, 'Area Affected': ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], 'Damage': 40000000.0, 'Deaths': 3103} , ....}
I am not showing you the entire dictionary because it is too long. You can try it yourself in your machine.
3. Write a function that converts the a new dictionary you just made from the previous question to another dictionary, where the keys are years and the values are lists containing a dictionary for each hurricane that occurred in that year.
For example, the key 1932 would yield the value:
[{'Name': 'Bahamas', 'Month': 'September', 'Year': 1932, 'Max Sustained Wind': 160, 'Areas Affected': ['The Bahamas', 'Northeastern United States'], 'Damage': 'Damages not recorded', 'Deaths': 16}, {'Name': 'Cuba II', 'Month': 'November', 'Year': 1932, 'Max Sustained Wind': 175, 'Areas Affected': ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], 'Damage': 40000000.0, 'Deaths': 3103}]
Solution:
So this question is a bit vague. It is not clear to us whether it wants us to use the dictionary which we got out of the previous function or we can just use the current lists we have right now to make up this new list.
As a programmer, I think you should be smart on which path is accessible and efficient for you. For me, I do not need to reinvent the wheels. I choose to use the current lists we were provided to construct this new dictionary.
My logic is for each year as a key I wanna create a list of dictionaries which contain all information about hurricanes occurring in that year. Therefore, if a year exists twice in the current year list, there should be two hurricane dictionaries. This means the number of each year occurrence in the list is the number of the dictionaries in the list as the values.
Here are my codes for this function. I wrote comments along the function. You should be able to understand what I was doing there.
You should also find a variable in there called index. I created this variable as a reminder for the function to know where the previous loop ended so it would know where to start to take the value for the next loop. Note that the reason I chose the strategy is that the original list has years sorted in order.
# write your construct hurricane by year dictionary function here:
def hurricane_by_year(names, months, years, winds, areas, damages, deaths):
#I use for loop to create a unique list for the year.
unique_year = []
for i in years:
if i not in unique_year:
unique_year.append(i)
else:
continue
#Then I counted how many time each year occurs in the year list
count_year = []
for i in unique_year:
count_year.append(years.count(i))
"""
Then we create an empty dictionary which wil store the final
dictonary we want.
Note that I also created another variable called index by giving
it a value of zero. This variable is very crucial for this function
to work.
"""
hurricane_year = {}
index = 0
for i, n in zip(unique_year, count_year):
hurricane_year[i] = []
for j in range(n):
hurricane_year[i].append({
"Name":names[index],
"Month":months[index],
"Year":years[index],
"Max Sustained Winds":winds[index],
"Areas Affected": areas[index],
"Damages": damages[index],
"Deaths": deaths[index]
})
index += 1
return hurricane_year
print(hurricane_by_year(names, months, years, max_sustained_winds, areas_affected, new_damages, deaths))
>>>[output]:
{1924: [{'Name': 'Cuba I', 'Month': 'October', 'Year': 1924, 'Max Sustained Winds': 165, 'Areas Affected': ['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], 'Damages': 'Damages not recorded', 'Deaths': 90}], 1928: [{'Name': 'San Felipe II Okeechobee', 'Month': 'September', 'Year': 1928, 'Max Sustained Winds': 160, 'Areas Affected': ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], 'Damages': 100000000.0, 'Deaths': 4000}], 1932: [{'Name': 'Bahamas', 'Month': 'September', 'Year': 1932, 'Max Sustained Winds': 160, 'Areas Affected': ['The Bahamas', 'Northeastern United States'], 'Damages': 'Damages not recorded', 'Deaths': 16}, {'Name': 'Cuba II', 'Month': 'November', 'Year': 1932, 'Max Sustained Winds': 175, 'Areas Affected': ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], 'Damages': 40000000.0, 'Deaths': 3103}], 1933: [{'Name': 'CubaBrownsville', 'Month': 'August', 'Year': 1933, 'Max Sustained Winds': 160, 'Areas Affected': ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], 'Damages': 27900000.0, 'Deaths': 179}, {'Name': 'Tampico', 'Month': 'September', 'Year': 1933, 'Max Sustained Winds': 160, 'Areas Affected': ['Jamaica', 'Yucatn Peninsula'], 'Damages': 5000000.0, 'Deaths': 184}],.....}
4. Write a function that counts how often each area is listed as an affected areas of a hurricane. Store and return the results in a dictionary where the keys are the affected areas and the values are counts of how many times the areas were affected.
Solution:
In other words, the project asks to count the occurrence of each location name from the area_affected list. If you look closely, you should see that area_affected list is a list of lists.
My strategy is to transform that list of lists into just a list, meaning appending the elements of the inner list into the outer list. Then I create an empty list to store the value of the unique location name from the new list. Next, I counted the occurrence of each location in the transformed list and stored them into an empty list. Finally, I combine the list of unique location names and the list of number of occurrences of each location into a dictionary the project requests.
Here are my codes.
# write your count affected areas function here:
def count_freq_area(areas):
list_all_area = []
for i in areas:
for j in i:
list_all_area.append(j)
unique_areas = []
for i in list_all_area:
if i not in unique_areas:
unique_areas.append(i)
else:
continue
count_area = []
for i in unique_areas:
count_area.append(list_all_area.count(i))
area_count_dict = {}
for area, count in zip(unique_areas, count_area):
area_count_dict[area] = count
return area_count_dict
test = count_freq_area(areas_affected)
print(test)
>>>[output]:
{'Central America': 9, 'Mexico': 7, 'Cuba': 6, 'Florida': 6, 'The Bahamas': 7, 'Lesser Antilles': 4, 'United States East Coast': 3, 'Atlantic Canada': 3, 'Northeastern United States': 2, 'Jamaica': 4, 'Cayman Islands': 1, 'Bermuda': 2, 'Texas': 4, 'Tamaulipas': 1, 'Yucatn Peninsula': 3, 'Georgia': 1, 'The Carolinas': 1, 'Virginia': 1, 'Southeastern United States': 1, 'Southwestern Quebec': 1, 'New England': 1, 'Louisiana': 1, 'Midwestern United States': 1, 'The Caribbean': 8, 'United States Gulf Coast': 6, 'United States East coast': 1, 'South Texas': 1, 'Venezuela': 3, 'Hispaniola': 1, 'South Florida': 1, 'Greater Antilles': 2, 'Bahamas': 2, 'Eastern United States': 1, 'Ontario': 1, 'Windward Islands': 1, 'Nicaragua': 1, 'Honduras': 1, 'Antilles': 1, 'Colombia': 1, 'Cape Verde': 1, 'British Virgin Islands': 1, 'U.S. Virgin Islands': 1, 'Virgin Islands': 1, 'Puerto Rico': 1, 'Dominican Republic': 1, 'Turks and Caicos Islands': 1, 'United States Gulf Coast (especially Florida Panhandle)': 1}
5. Write a function that finds the area affected by the most hurricanes, and how often it was hit..
Solution:
Question 5 requires us to use the dictionary we get from question 4 function. It asks for the name of the location affected by the most hurricanes. In other words, which location do hurricanes occur the most?
So the strategy is to go through the key-value pairs of the dictionary in the previous question, and then return the name of the location paired with the highest number of hurricane occurrences.
I create a variable, called key_max_area, which takes a string “Florida”. This is actually very random. You can use any name of the location for this variable. I also create another variable, value_max_count, to take the value of zero. These two variables are essential for the function.
As we loop through the key of area_dict dictionary, we set a condition, if a value of a key of area_dict is bigger than value_max_count, then value_max_count will be assigned to that value and key_max_area will take that key as a new string.
Once the loop finishes, we will have the key-value pair which has the highest number of occurrences along with the name of the location.
# write your find most affected area function here:
def most_freq_area(areas_dict):
key_max_area = "Florida"
value_max_count = 0
for key in areas_dict:
if areas_dict[key] > value_max_count:
key_max_area = key
value_max_count = areas_dict[key]
result = {key_max_area:value_max_count}
return result
print(most_freq_area(test))
>>>[output]:
{'Central America': 9}
6. Write a function that finds the hurricane that caused the greatest number of deaths, and how many deaths it caused.
Solution:
This question is very similar to question 5. However, l modify the project question to be a bit more challenging. Instead of returning the name and its highest value, the function will return a dictionary where the key is the name of the location and the value is the number of deaths. And the dictionary will be sorted by the damage from highest to lowest.
# write your greatest number of deaths function here:
def most_deaths_hurricane(hurricane):
for_pop_dict = dict(hurricane)
sort_list = []
for value in for_pop_dict.values():
sort_list.append(value['Deaths'])
sort_list.sort(reverse=True)
sort_dict = {}
for i in sort_list:
for k in for_pop_dict.keys():
if for_pop_dict[k]['Deaths'] == i:
sort_dict[k] = i
for_pop_dict.pop(k)
break
return sort_dict
test2 = most_deaths_hurricane(hurricane_records)
print(test2)
>>>[output]:
{'Mitch': 19325, 'San Felipe II Okeechobee': 4000, 'Cuba II': 3103, 'Maria': 3057, 'David': 2068, 'Katrina': 1836, 'Janet': 1023, 'Beulah': 688, 'New England': 682, 'Matthew': 603, 'Labor Day': 408, 'Hattie': 319, 'Gilbert': 318, 'Allen': 269, 'Camille': 259, 'Tampico': 184, 'CubaBrownsville': 179, 'Irma': 138, 'Felix': 133, 'Rita': 125, 'Ivan': 124, 'Hugo': 107, 'Cuba I': 90, 'Wilma': 87, 'Michael': 74, 'Andrew': 65, 'Isabel': 51, 'Dean': 45, 'Carla': 43, 'Edith': 37, 'Emily': 17, 'Bahamas': 16, 'Anita': 11, 'Carol': 5}
7. Write a function that that rates hurricanes on mortality scale according to the following rating, where the key is the rating and the value is the upper bound of deaths for that rating.
mortality_scale = {0: 0,
1: 100,
2: 500,
3: 1000,
4: 10000}
For example, a hurricane with a 1 mortality rating would have resulted in greater than 0 but less than or equal to 100 deaths. A hurricane with a 5 mortality rating would have resulted in greater than 10000 deaths.
Store the hurricanes in a new dictionary where the keys are mortality ratings and the values are lists containing a dictionary for each hurricane that falls into that mortality rating.
Solution:
If you read the last part of the question carefully, you start to notice that it asks us to create a dictionary where the values are lists containing another dictionary.
I wrote comments in the code so that you could see what I was doing in there.
# write your catgeorize by mortality function here:
def mortality_scale(hurricane):
#I created an empty dictionary to store our answer
scale_mortality = {}
"""
I created 5 more dictionaries which will be the inner dictionary
of a list. That list will be the value for the key, mortality rate.
"""
inner_dict_0 = {}
inner_dict_100 = {}
inner_dict_500 = {}
inner_dict_1000 = {}
inner_dict_10000 = {}
"""
I looped through key value pairs of hurrincane dictionary.
As I did that, I wrote conditions to clasify the hurricanes based on
mortality rate and update into the empty inner dictionaries I created
eariler.
"""
for key, value in hurricane.items():
if value['Deaths'] == 0:
inner_dict_0.update({key:value})
elif value['Deaths'] <= 100:
inner_dict_100.update({key:value})
elif value['Deaths'] <= 500:
inner_dict_500.update({key:value})
elif value['Deaths'] <= 1000:
inner_dict_1000.update({key:value})
elif value['Deaths'] <= 10000:
inner_dict_10000.update({key:value})
"""
I created a list of dictionary so that I could perform for loop
so that I could construct the final answer.
"""
list_scale_dict = [inner_dict_0, inner_dict_100, inner_dict_500, inner_dict_1000, inner_dict_10000]
for i in range(5):
scale_mortality[i] = [list_scale_dict[i]]
return scale_mortality
print(mortality_scale(hurricane_records))
The answer is too long. You may try the code yourself.
8. Write a function that finds the hurricane that caused the greatest damage, and how costly it was.
Solution:
This question is very similar to question 5. The difference is that question 8 asks us to write a function to find the name of the hurricane which caused the most damage rather than most occurrences. Because the damages list has strings in it, we will have to do some extra work, which is to convert the string “Damages not recorded” to zero (so that we could sort them).
I did a slight change of taste. Instead of returning a dictionary of this key-value pair, I return two values. Here is my code. It shared the same strategy with question 5.
# write your greatest damage function here:
def greatest_damage(hurricane):
max_key = ""
max_value = 0
for key in hurricane.keys():
if hurricane[key]['Damage'] == "Damages not recorded":
hurricane[key]['Damage'] = 0
for key in hurricane.keys():
if hurricane[key]['Damage'] > max_value:
max_key = key
max_value = hurricane[key]['Damage']
return max_key, max_value
print(greatest_damage(hurricane_records))
>>>[output]:
('Katrina', 125000000000.0)
9. Write a function that rate hurricanes on damage scale according to the following ratings, where the key is the rating and the value is the upper bound of damage for that rating.
damage_scale = {0: 0,
1: 100000000,
2: 1000000000,
3: 10000000000,
4: 50000000000}
For example, a hurricane with a 1 damage rating would have resulted in damages greater than 0 USD but less than or equal to 100000000 USD. A hurricane with a 5 damage rating would have resulted in damages greater than 50000000000 USD.
Store the hurricanes in a new dictionary where the keys are damage ratings and the values are lists containing a dictionary for each hurricane that falls into that damage rating.
Solution:
Once again, this last question is also similar to question 7, same strategy. I am going to share my code here. You can print the answer in your local machine.
# write your catgeorize by damage function here:
def damage_scale(hurricane):
scale_damage = {}
inner_dict_0 = {}
inner_dict_100000000 = {}
inner_dict_1000000000 = {}
inner_dict_10000000000 = {}
inner_dict_50000000000 = {}
for key, value in hurricane.items():
if value['Damage'] == 'Damages not recorded':
inner_dict_0.update({key:value})
elif value['Damage'] <= 100000000:
inner_dict_100000000.update({key:value})
elif value['Damage'] <= 1000000000:
inner_dict_1000000000.update({key:value})
elif value['Damage'] <= 10000000000:
inner_dict_10000000000.update({key:value})
elif value['Damage'] <= 50000000000:
inner_dict_50000000000.update({key:value})
list_scale_dict = [inner_dict_0, inner_dict_100000000, inner_dict_1000000000, inner_dict_10000000000, inner_dict_50000000000]
for i in range(5):
scale_damage[i] = [list_scale_dict[i]]
return scale_damage
print(damage_scale(hurricane_records))