how to get average and total values for each year in python

63 views Asked by At

i have a list of lists like below.

[('2017-12-01', ['5', '6', '0', False]), 
 ('2017-12-02', ['5', '7', '0', False]), 
 ('2017-12-03', ['6', '7', '0.5', True]), 
 ('2017-12-04', ['6', '7', '0.5', True]), 
 ('2017-12-05', ['5', '6', '0.4', True]), 
 ('2018-01-01', ['5', '6', '0', False]), 
 ('2018-01-02', ['5', '6', '0', False])]

the first element indicate the date and the second lists indicate the highest tem,the lowest temp the precipitation and the boolian indicate whether there is rain or not.i want to get the average of the the highest temperature and the average of the lowest temperature for each year.it is easy to get for the whole list.But the difficult thing is to get the average for each year.

1

There are 1 answers

0
Scott Hunter On

This will split up your data by year:

by_year = {}
for d,x in data:
    y = d.split("-")[0]
    if y in by_year:
        by_year[y].append(x)
    else:
        by_year[y] = [x]