Search code examples
pythonpython-itertools

how to extract summary list from itertools groupby for statement


Using python 3.2 In a nested list are two columns of input data: a species code in [0] and an observed value in [1] and from each row two additional columns, [2] and [3], of calculated values, for example:

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

I needed, and got (with help) the subtotals in [2] and [3] for each species code [0] by:

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

Now I need to make another list, call it summary_trees, with just these values in it so I can use it elsewhere. It this example it would be:

summary_trees=[[ACRU, 3, 21],[QURU, 7, 25]]

It seems like this should be simple and I feel dumb not to see it. In real life there is an indeterminate number of species codes, usually between 4-8


Solution

  • It seems as simple as building a list with your printed output...

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