[Python-ideas] SQL-like way to manipulate Python data structures

Arnaud Delobelle arno at marooned.org.uk
Sat May 26 18:58:53 CEST 2007


On 26 May 2007, at 17:06, Steve Howell wrote:

> I find myself often writing somewhat tedious code in
> Python that is just slicing/aggregating fairly simple
> data structures like a list of dictionaries.
>
> Here is something that I'm trying to express, roughly:
>
>    charges =
>        sum float(charge) from events
>        on setup, install
>        group by name

I don't really understand that :)

> Here is the Python code that I wrote:
>
>     def groupDictBy(lst, keyField):
>         dct = {}
>         for item in lst:
>             keyValue = item[keyField]
>             if keyValue not in dct:
>                 dct[keyValue] = []
>             dct[keyValue].append(item)
>         return dct
>

isn't that itertools.groupby?

>     dct = groupDictBy(events, 'name')
>     for name in dct:
>         events = dct[name]
>         charges = {}
>         for bucket in ('setup', 'install'):
>             charges[bucket] = sum(
>                     [float(event['charge']) for
>                     event in events
>                     if event['bucket'] == bucket])

from itertools import groupby
from operator import itemgetter

charges = {}
for name, n_evts in groupby(events, itemgetter('name')):
     charges[name] = dict((bucket, sum(float(e['charge']) for e in  
b_evts)
     	for bucket, b_evts in groupby(n_evts, itemgetter('bucket'))
         if bucket in ('setup', 'install'))

The last line can be omitted if the only two existing buckets are
'setup' and 'install'.

Untested!  I've changed your code slightly as in your snippet 'charges'
is reset to {} at each iteration of the loop.

-- 
Arnaud





More information about the Python-ideas mailing list