BERA BERA - 17 days ago 5
Python Question

Python group and sum

I have a list of tuples:

Listoftuples=[
(0.021892733407683305, 0.14887058717224647, 4.573173081530965, 0.04619366749021177, u'0102'),
(0.08416364174734663, 0.8500527816482009, 23.649983331004403, 0.0, u'0103'),
(0.02181070623592521, 0.15049387302788395, 1.2098398749067714, 1.6037412295275804, u'0102')
]


I want to per group (group = last value in tuple, for example u'0102'):


  • Summarize first value

  • Summarize second value and divide by sum of first value



Attempt:

import itertools

Listoftuples=[
(0.021892733407683305, 0.14887058717224647, 4.573173081530965, 0.04619366749021177, u'0102'),
(0.08416364174734663, 0.8500527816482009, 23.649983331004403, 0.0, u'0103'),
(0.02181070623592521, 0.15049387302788395, 1.2098398749067714, 1.6037412295275804, u'0102')
]

keyfunc=lambda t: (t[4])
Listoftuples.sort(key=keyfunc)

for key,rows in itertools.groupby(Listoftuples, keyfunc):
sumOfFirstValue = sum(r[0] for r in rows)
sumOfSecondDividedBySumOfFirst= sum(r[1] for r in rows)/sumOfFirstValue
print key,sumOfFirstValue,sumOfSecondDividedBySumOfFirst


Results:

0102 0.0437034396436 0.0
0103 0.0841636417473 0.0


Zero values in last. How can i fix it?

Answer

A common mistake is to think that rows returned from groupby is a concrete list. Actually it's an iterator and is exhausted while calculating sumOfFirstValue. A workaround is:

...
for key,rows in itertools.groupby(Listoftuples, keyfunc):
    rows = list(rows)
    ...