list - Need to take average of long, strangely formatted set of data in Python -


so have large set of data looks

[('art', [100, 234, 830, 304]), ('math', [600, 1400, 300, 340]), ('history', [2010, 300,   400, 600])] 

how turn set of data can average numbers inside of , sort? i'm using python 2.7

for:

input = [('art', [100, 234, 830, 304]),           ('math', [600, 1400, 300, 340]),           ('history', [2010, 300, 400, 600])] 

this:

print sorted( ( (k,sum(v)/len(v)) k,v in input ), key=lambda t: t[1] ) 

prints:

[('art', 367), ('math', 660), ('history', 827)] 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -