python - Calculating average from ith element in list of tuples -
note: i've done edits dataset think work better calculating average.
my data this:
[[2015, 0.23, 0.45, 1.23], [2014, 1.22, 2.54, 0.98], [2013, 0.33, 2.11, 0.43], ...] where data lists within list. first element in sublist year of growth, , following 3 floats ring widths year. question this: there way calculate average of last 3 elements in each sublist, , generate new list, year, average growth like:
[[2015, 1.58], [2014, 0.956], ...] where 1.58 (1.22 + 2.54 + 0.98)/3.
sorry confusion before, i'm new stack overflow, , can tell more information better.
here code have far:
in list[0]: newlist = [(list[1] + list[2] + list[3])/3] but doesn't seem work.
any appreciated. thanks!
if data in form:
trees= [[2015, 0.23, 0.45, 1.23], [2014, 1.22, 2.54, 0.98], [2013, 0.33, 2.11, 0.43]] you do:
new_trees=[] x in trees: average="{0:.2f}".format(sum(x[1:])/len(x[1:])) new_trees.append([x[0]]+[average]) #year + average this output:
>>> new_trees [[2015, '0.64'], [2014, '1.58'], [2013, '0.96']]
Comments
Post a Comment