Confused as to how to create this final part of my function.
The function below needs to read in data, start year and end year.
Consider the following:
def summary_statistics(data, year_start, year_end):
earthquake_count_by_year = []
total_damages_by_year = []
casualties_by_year = []
year_count = {}
dmg_sum = {}
casualties = {}
years = []
year_start = int(year_start)
year_end = int(year_end)
if year_end >= year_start:
#store year range into list
years = list(range(year_start, year_end+1))
for index, tuple in enumerate(data):
#values to be summed for each year in dmg_sum
yr = tuple[0]
dmgs = tuple[9]
deaths = tuple[6]
missing = tuple[7]
injured = tuple[8]
#if year in range of years (year_start - year_end)
if tuple[0] in years:
#if year does not exist in dict, set it to 0
if tuple[0] not in year_count:
year_count[tuple[0]] = 0
#count number of occurences for each year
year_count[tuple[0]] += 1
if tuple[0] not in dmg_sum:
dmg_sum[tuple[0]] = dmgs
else:
dmg_sum[tuple[0]] += dmgs
if tuple[0] not in casualties:
casualties[tuple[0]] = list(deaths + ',' + missing + ',' + injured)
else:
casualties[tuple[0]] += list(deaths + ',' + missing + ',' + injured)
earthquake_count_by_year = list(year_count.items())
total_damages_by_year = list(dmg_sum.items())
casualties_by_year = list(casualties.items())
L = [[earthquake_count_by_year], [total_damages_by_year], [casualties_by_year]]
print(L)
return L
The part I am having trouble with is:
if tuple[0] not in casualties:
casualties[tuple[0]] = list(deaths + ',' + missing + ',' + injured)
else:
casualties[tuple[0]] += list(deaths + ',' + missing + ',' + injured)
This is the expected output I need for casualties_by_year[]:
[(2020, (deaths, missing, injured)), (2019, (deaths, missing, injured)), (2018, (deaths, missing, injured))]
So what I'm trying to do is construct a list of tuples, in the arrangement shown above, for the final list going into L. it's tuple[0] for year, tuple[6] for deaths, tuple[7] for missing, tuple[8] for injured.
How can I correct that final if/else to get the list of tuples I'm looking for?
List comprehension?
EDIT- This is what "data" looks as it's input into the function:
[(2020, 1, 6.0, 'CHINA: XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), (2020, 1, 6.7, 'TURKEY: ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), (2020, 1, 7.7, 'CUBA: GRANMA; CAYMAN IS; JAMAICA', 19.44, -78.755, 0, 0, 0, 0), (2020, 2, 6.0, 'TURKEY: VAN; IRAN', 38.482, 44.367, 10, 0, 60, 0), (2020, 3, 5.4, 'BALKANS NW: CROATIA: ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0)]