Hurricane Analysis Project Step 4

Hello! I’m working on step 4 in the Hurricane Analysis project. I’m confused by part of the hint and the subsequent code in the solution:

The hint:

Our function iterates through each hurricane in our hurricanes dictionary, hurricanes , and records the year as current_year and the hurricane dictionary as current_cane .

We then check if current_year exists as a key in our new dictionary, and if not, initialize the value for that key [current_cane] .

If current_year does exist as a key, current_cane is appended to the list stored in the key.

– What’s the purpose of initializing the value for that key ‘current_cane]’.

Here’s the code:
def create_year_dictionary(hurricanes):
“”“Convert dictionary with hurricane name as key to a new dictionary with hurricane year as the key and return new dictionary.”""
hurricanes_by_year= dict()
for cane in hurricanes:
current_year = hurricanes[cane][‘Year’]
current_cane = hurricanes[cane]
if current_year not in hurricanes_by_year:
** hurricanes_by_year[current_year] = [current_cane]**
else:
hurricanes_by_year[current_year].append(current_cane)
return hurricanes_by_year

And a link the the project:

Project

Is this what your hurricanes dictionary looks like?

{
'Cuba I': {'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}, 
'San Felipe II Okeechobee': {'Name': 'San Felipe II Okeechobee', 'Month': 'September', 'Year': 1928, 'Max Sustained  Wind': 160, 'Areas 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, 'Areas 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, 'Areas Affected': ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], 'Damage': 40000000.0, 'Deaths': 3103}, 
'CubaBrownsville': {'Name': 'CubaBrownsville', 'Month': 'August', 'Year': 1933, 'Max Sustained  Wind': 160, 'Areas Affected': ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], 'Damage': 27900000.0, 'Deaths': 179}, 'Tampico': {'Name': 'Tampico', 'Month': 'September', 'Year': 1933, 'Max Sustained  Wind': 160, 'Areas Affected': ['Jamaica', 'Yucatn Peninsula'], 'Damage': 5000000.0, 'Deaths': 184}, 
'Labor Day': {'Name': 'Labor Day', 'Month': 'September', 'Year': 1935, 'Max Sustained  Wind': 185, 'Areas Affected': ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], 'Damage': 'Damages not recorded', 'Deaths': 408}, 
'New England': {'Name': 'New England', 'Month': 'September', 'Year': 1938, 'Max Sustained  Wind': 160, 'Areas Affected': ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], 'Damage': 306000000.0, 'Deaths': 682}, 
'Carol': {'Name': 'Carol', 'Month': 'September', 'Year': 1953, 'Max Sustained  Wind': 160, 'Areas Affected': ['Bermuda', 'New England', 'Atlantic Canada'], 'Damage': 2000000.0, 'Deaths': 5}, 
'Janet': {'Name': 'Janet', 'Month': 'September', 'Year': 1955, 'Max Sustained  Wind': 175, 'Areas Affected': ['Lesser Antilles', 'Central America'], 'Damage': 65800000.0, 'Deaths': 1023}, 
'Carla': {'Name': 'Carla', 'Month': 'September', 'Year': 1961, 'Max Sustained  Wind': 175, 'Areas Affected': ['Texas', 'Louisiana', 'Midwestern United States'], 'Damage': 326000000.0, 'Deaths': 43}, 
'Hattie': {'Name': 'Hattie', 'Month': 'October', 'Year': 1961, 'Max Sustained  Wind': 160, 'Areas Affected': ['Central America'], 'Damage': 60300000.0, 'Deaths': 319}, 
'Beulah': {'Name': 'Beulah', 'Month': 'September', 'Year': 1967, 'Max Sustained  Wind': 160, 'Areas Affected': ['The Caribbean', 'Mexico', 'Texas'], 'Damage': 208000000.0, 'Deaths': 688}, 
'Camille': {'Name': 'Camille', 'Month': 'August', 'Year': 1969, 'Max Sustained  Wind': 175, 'Areas Affected': ['Cuba', 'United States Gulf Coast'], 'Damage': 1420000000.0, 'Deaths': 259}, 
'Edith': {'Name': 'Edith', 'Month': 'September', 'Year': 1971, 'Max Sustained  Wind': 160, 'Areas Affected': ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], 'Damage': 25400000.0, 'Deaths': 37}, 
'Anita': {'Name': 'Anita', 'Month': 'September', 'Year': 1977, 'Max Sustained  Wind': 175, 'Areas Affected': ['Mexico'], 'Damage': 'Damages not recorded', 'Deaths': 11}, 
'David': {'Name': 'David', 'Month': 'August', 'Year': 1979, 'Max Sustained  Wind': 175, 'Areas Affected': ['The Caribbean', 'United States East coast'], 'Damage': 1540000000.0, 'Deaths': 2068}, 
'Allen': {'Name': 'Allen', 'Month': 'August', 'Year': 1980, 'Max Sustained  Wind': 190, 'Areas Affected': ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], 'Damage': 1240000000.0, 'Deaths': 269}, 
'Gilbert': {'Name': 'Gilbert', 'Month': 'September', 'Year': 1988, 'Max Sustained  Wind': 185, 'Areas Affected': ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], 'Damage': 7100000000.0, 'Deaths': 318}, 
'Hugo': {'Name': 'Hugo', 'Month': 'September', 'Year': 1989, 'Max Sustained  Wind': 160, 'Areas Affected': ['The Caribbean', 'United States East Coast'], 'Damage': 10000000000.0, 'Deaths': 107}, 'Andrew': {'Name': 'Andrew', 'Month': 'August', 'Year': 1992, 'Max Sustained  Wind': 175, 'Areas Affected': ['The Bahamas', 'Florida', 'United States Gulf Coast'], 'Damage': 26500000000.0, 'Deaths': 65}, 
'Mitch': {'Name': 'Mitch', 'Month': 'October', 'Year': 1998, 'Max Sustained  Wind': 180, 'Areas Affected': ['Central America', 'Yucatn Peninsula', 'South Florida'], 'Damage': 6200000000.0, 'Deaths': 19325}, 
'Isabel': {'Name': 'Isabel', 'Month': 'September', 'Year': 2003, 'Max Sustained  Wind': 165, 'Areas Affected': ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], 'Damage': 5370000000.0, 'Deaths': 51}, 
'Ivan': {'Name': 'Ivan', 'Month': 'September', 'Year': 2004, 'Max Sustained  Wind': 165, 'Areas Affected': ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], 'Damage': 23300000000.0, 'Deaths': 124}, 
'Emily': {'Name': 'Emily', 'Month': 'July', 'Year': 2005, 'Max Sustained  Wind': 160, 'Areas Affected': ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], 'Damage': 1010000000.0, 'Deaths': 17}, 
'Katrina': {'Name': 'Katrina', 'Month': 'August', 'Year': 2005, 'Max Sustained  Wind': 175, 'Areas Affected': ['Bahamas', 'United States Gulf Coast'], 'Damage': 125000000000.0, 'Deaths': 1836}, 
'Rita': {'Name': 'Rita', 'Month': 'September', 'Year': 2005, 'Max Sustained  Wind': 180, 'Areas Affected': ['Cuba', 'United States Gulf Coast'], 'Damage': 12000000000.0, 'Deaths': 125}, 
'Wilma': {'Name': 'Wilma', 'Month': 'October', 'Year': 2005, 'Max Sustained  Wind': 185, 'Areas Affected': ['Greater Antilles', 'Central America', 'Florida'], 'Damage': 29400000000.0, 'Deaths': 87}, 
'Dean': {'Name': 'Dean', 'Month': 'August', 'Year': 2007, 'Max Sustained  Wind': 175, 'Areas Affected': ['The Caribbean', 'Central America'], 'Damage': 1760000000.0, 'Deaths': 45}, 'Felix': {'Name': 'Felix', 'Month': 'September', 'Year': 2007, 'Max Sustained  Wind': 175, 'Areas Affected': ['Nicaragua', 'Honduras'], 'Damage': 720000000.0, 'Deaths': 133}, 
'Matthew': {'Name': 'Matthew', 'Month': 'October', 'Year': 2016, 'Max Sustained  Wind': 165, 'Areas Affected': ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], 'Damage': 15100000000.0, 'Deaths': 603}, 
'Irma': {'Name': 'Irma', 'Month': 'September', 'Year': 2017, 'Max Sustained  Wind': 180, 'Areas Affected': ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], 'Damage': 64800000000.0, 'Deaths': 138}, 
'Maria': {'Name': 'Maria', 'Month': 'September', 'Year': 2017, 'Max Sustained  Wind': 175, 'Areas Affected': ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], 'Damage': 91600000000.0, 'Deaths': 3057}, 
'Michael': {'Name': 'Michael', 'Month': 'October', 'Year': 2018, 'Max Sustained  Wind': 160, 'Areas Affected': ['Central America', 'United States Gulf Coast (especially Florida Panhandle)'], 'Damage': 25100000000.0, 'Deaths': 74}
}
print (hurricanes['Katrina'].get('Year'))    #  2005
1 Like

Thanks for you reply, mtf. Yep, that’s what my hurricane dictionary looks like.

Alright, that’s where we can start. All we need to do is create a new dictionary, with the years as primary keys. This is where we have to stop and think… Are there any hurricanes that occur in the same year?

1 Like

:slight_smile: Thanks, mtf.

I spent the last couple of hours working through the logic. (Should it really take me this long??) (If I started by adding print statements for each variable, I probably could have worked it out sooner… maybe.) (I need a drink.) (It’s only 10.30 a.m.)

Is this correct?

hurricanes_by_year is the new dictionary.
The for loop iterates through hurricanes and assigns the year to current_year and the hurricane name to current_cane.
It checks hurricanes_by_year to see if the current hurricane is in there. If it’s not, the current hurricane is added, with the key set to the year (hurricanes_by_year[current_year] ).

If the year already exists in hurricanes_by_year, then that means the current hurricane happened the same year as another and its dictionary needs to be added to the key for that year.

Thanks again!!
Kelly

2 Likes

Please show us the structure of your new hurricanes_by_year dictionary. (Just the structure, not the data, to be clear.)

Yes, especially if it meant a lot of digging, research and practice. If it came easy every cheater would cake walk through it.

We need to trust that we can come up with logic, and then turn around and not trust that logic. Only a serious learner can self-criticize. It all takes time. There is no clock on this.

2 Likes

What do you mean by structure exactly?

Like this?

{Year: [{Name:
{Month:
{Year:
{Max Sustained Wind:
{Areas Affected: [ ]
{Damage:
{Deaths: }]

1 Like

Pretty much. The shapes are all that concern us. A dictionary with keys to lists of dictionaries is the shape one was expecting. Looks like you caught on so no data got left out.

What logic did you follow in this step of construction? Did you end up with lists of dictionaries, as planned?

1 Like

Yes, but only because I reversed engineered the solution. I don’t think I could have done it without doing that first.

I do think that helped me understand the structure and how to achieve it, so hopefully I have a shot at completing the rest of the project without having to go to the solution so soon!

3 Likes