Hurricane Analysis Challenge Project (Python)

Finally completed this project. One of the toughest ones I have done so far, but feel that I learned a lot about dictionaries.

Here is my code:

My code

# 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]

# 1
# Update Recorded Damages
conversion = {"M": 1000000,
              "B": 1000000000}
def convert_damages(lst):
  new_list = []
  for a in lst:
    if a[-1] == "M":
      new_list.append(float(a[:-1])*1000000)
    elif a[-1] == "B":
      new_list.append(float(a[:-1])*1000000000)
    else:
      new_list.append(a)
  return new_list

# test function by updating damages
updated_damages = convert_damages(damages)
#print(updated_damages)

# 2 
# Create a Table
def create_dict(name, month, year, max_wind, areas_affected, damage, death):
  new_dict = {}
  for i in range(len(name)):
    new_dict[name[i]] = {'Name': name[i], 'Month': month[i], 'Year': year[i], 'Max Sustained Wind': max_wind[i], 'Areas Affected': areas_affected[i], 'Damage': damage[i], 'Deaths': death[i]}
  return new_dict
# Create and view the hurricanes dictionary
hurricane_dict = create_dict(names, months, years, max_sustained_winds, areas_affected, damages, deaths)
#print(hurricane_dict)
# 3
# Organizing by Year
def organize_by_year(new_dict):
  year_list = sorted(set([new_dict[a]['Year'] for a in new_dict.keys()]))
  hurricane_list = list(new_dict)  
  final_dict = {}
  for a in year_list:
    for b in hurricane_list:
      if new_dict[b]['Year'] == a:
        if not a in final_dict:
          final_dict[a] = [new_dict[b]]
        else:
          final_dict[a].append(new_dict[b])
  return final_dict

# create a new dictionary of hurricanes with year and key
hurricanes_by_year = organize_by_year(hurricane_dict)
#print(hurricanes_by_year)
# 4
# Counting Damaged Areas
def count_damaged_area(new_dict):
  final_dict = {}
  for a in new_dict.keys():
    for b in new_dict[a]['Areas Affected']:
      if not b in final_dict:
        final_dict[b] = 1
      else:
        final_dict[b] += 1
  return final_dict


# create dictionary of areas to store the number of hurricanes involved in
affected_areas = count_damaged_area(hurricane_dict)
#print(affected_areas)


# 5 
# Calculating Maximum Hurricane Count
def max_affected_area(new_dict):
  count_list = sorted(set(list(new_dict.values())))
  final_dict = {}
  for a in new_dict.keys():
    if count_list[-1] == new_dict[a]:
      final_dict[a] = new_dict[a]
  return final_dict

# find most frequently affected area and the number of hurricanes involved in

check_max_affected_area = max_affected_area(affected_areas)
#print(check_max_affected_area)

# 6
# Calculating the Deadliest Hurricane
def find_hurricane_with_most_deaths(new_dict):
  death_list = [new_dict[a]['Deaths'] for a in new_dict.keys()]
  death_list.sort(reverse = True)
  final_dict = {}
  for a in new_dict.keys():
    if new_dict[a]['Deaths'] == death_list[0]:
      final_dict[a] = new_dict[a]['Deaths']
  return final_dict



# find highest mortality hurricane and the number of deaths
deadliest_hurricane = find_hurricane_with_most_deaths(hurricane_dict)
#print(deadliest_hurricane)
# 7
# Rating Hurricanes by Mortality
def rate_mortality(some_number):
  if some_number == 0:
    return 0
  elif some_number > 0 and some_number <= 100:
    return 1
  elif some_number > 100 and some_number <= 500:
    return 2
  elif some_number > 500 and some_number <= 1000:
    return 3
  elif some_number > 1000 and some_number <= 10000:
    return 4
  else:
    return 5

def rate_hurricane_mortality(new_dict):
  final_dict = {}
  for a in new_dict.keys():
    current_hurricane_rating = rate_mortality(new_dict[a]['Deaths'])
    if not current_hurricane_rating in final_dict:
      final_dict[current_hurricane_rating] = [new_dict[a]]
    else:
      final_dict[current_hurricane_rating].append(new_dict[a])
  final_dict1 = {}
  final_list = sorted(list(final_dict.keys()))
  for a in final_list:
    final_dict1[a] = final_dict[a]
  return final_dict1
  

# categorize hurricanes in new dictionary with mortality severity as key
hurricanes_categorized = rate_hurricane_mortality(hurricane_dict)
#print(hurricanes_categorized)

# 8 Calculating Hurricane Maximum Damage
def convert_number(some_number):
  if some_number[-1] == "M":
    return float(some_number[:-1])*1000000
  elif some_number[-1] == "B":
    return float(some_number[:-1]) * 1000000000
  else:
    return 0

#print(hurricane_dict)
def find_most_damaging_hurricane(new_dict):
  damage_list = [convert_number(new_dict[a]['Damage']) for a in new_dict.keys()]
  max_damage = 0
  max_index = 0
  for a in range(len(damage_list)):
    if damage_list[a] > max_damage:
      max_damage = damage_list[a]
      max_index = a
  final_key = list(new_dict)[max_index]
  return {final_key: max_damage}

# find highest damage inducing hurricane and its total cost
most_damaging_hurricane = find_most_damaging_hurricane(hurricane_dict)
#print(most_damaging_hurricane)

# 9
# Rating Hurricanes by Damage
#print(hurricane_dict)
damage_scale = {0: 0,
                1: 100000000,
                2: 1000000000,
                3: 10000000000,
                4: 50000000000}
  
# categorize hurricanes in new dictionary with damage severity as key
def categorize_on_damage(some_number):
  if some_number == 0:
    return 0
  elif some_number > 0 and some_number <= 100000000:
    return 1
  elif some_number > 100000000 and some_number <= 1000000000:
    return 2
  elif some_number > 1000000000 and some_number <= 10000000000:
    return 3
  elif some_number > 10000000000 and some_number <= 50000000000:
    return 4
  else:
    return 5

def categorize_hurricanes_on_damage(new_dict):
  damage_list = [convert_number(new_dict[a]['Damage']) for a in new_dict.keys()]
  category_list = [categorize_on_damage(a) for a in damage_list]
  hurricane_list = list(new_dict)
  final_dict = {}
  for a in range(len(category_list)):
    if not category_list[a] in final_dict.keys():
      final_dict[category_list[a]] = [new_dict[hurricane_list[a]]]
    else:
      final_dict[category_list[a]].append(new_dict[hurricane_list[a]])
  return final_dict

hurricanes_based_on_categories = categorize_hurricanes_on_damage(hurricane_dict)
print(hurricanes_based_on_categories)







My code. It’s probably not the most efficient, but since I’m learning, I think that’s ok for now XD. The reduced guidance was very helpful - helped me realize I really don’t know what I think I know and go back and relearn some of the concepts