python - how to extract summary list from itertools groupby for statement -


using python 3.2 in nested list 2 columns of input data: species code in [0] , observed value in [1] , each row 2 additional columns, [2] , [3], of calculated values, example:

sorted_trees=[ ['acru', 5, 1, 10], ['acru', 6, 2, 11], ['quru', 7, 3, 12], ['quru', 8, 4, 13]] 

i needed, , got (with help) subtotals in [2] , [3] each species code [0] by:

import itertools it, operator op k,g in it.groupby(sorted_trees, key=op.itemgetter(0)):     tempg=list(g)     print(k, sum(i[2] in tempg), sum(i[3] in tempg)) 

now need make list, call summary_trees, these values in can use elsewhere. example be:

summary_trees=[[acru, 3, 21],[quru, 7, 25]] 

it seems should simple , feel dumb not see it. in real life there indeterminate number of species codes, between 4-8

it seems simple building list printed output...

import itertools it, operator op summary_trees = [] k,g in it.groupby(sorted_trees, key=op.itemgetter(0))     tempg=list(g)     summary_trees.append([k, sum(i[2] in tempg), sum(i[3] in tempg)]) 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -