Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Group by and sum a list of tuples

Given the following list:

[
    ('A', '', Decimal('4.0000000000'), 1330, datetime.datetime(2012, 6, 8, 0, 0)),
    ('B', '', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 6, 4, 0, 0)),
    ('AA', 'C', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 5, 31, 0, 0)),
    ('B', '', Decimal('7.0000000000'), 1330, datetime.datetime(2012, 5, 24, 0, 0)),
    ('A', '', Decimal('21.0000000000'), 1330, datetime.datetime(2012, 5, 14, 0, 0))
]

I would like to group these by the first, second, fourth and fifth columns in the tuple and sum the 3rd. For this example I'll name the columns as col1, col2, col3, col4, col5.

In SQL I would do something like this:

select col1, col2, sum(col3), col4, col5 from my table
group by col1, col2, col4, col5

Is there a "cool" way to do this or is it all a manual loop?

like image 348
jbassking10 Avatar asked Jun 15 '12 20:06

jbassking10


People also ask

How do you sum a list of tuples in Python?

To sum a list of tuples element-wise: Use the zip function to get an iterator of tuples with the corresponding items. Use a list comprehension to iterate over the iterable. On each iteration, pass the tuple to the sum() function.

Can you sum tuples in Python?

Use the sum() function to sum the elements of a tuple, e.g. result = sum(my_tuple) . The sum function takes an iterable (such as a tuple) as an argument, sums its items from left to right and returns the total.


1 Answers

You want itertools.groupby.

Note that groupby expects the input to be sorted, so you may need to do that before hand:

keyfunc = lambda t: (t[0], t[1], t[3], t[4])
data.sort(key=keyfunc)
for key, rows in itertools.groupby(data, keyfunc):
    print key, sum(r[2] for r in rows)
like image 128
David Wolever Avatar answered Sep 22 '22 21:09

David Wolever