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

Steve Howell showell30 at yahoo.com
Sat May 26 18:06:25 CEST 2007


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

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

    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])

Comments are welcome on improving the code itself, but
I wonder if Python 3k (or 4k?) couldn't have some kind
of native SQL-like ways of manipulating lists and
dictionaries.  I don't have a proposal myself, just
wonder if others have felt this kind of pain, and
maybe it will spark a Pythonic solution.




       
____________________________________________________________________________________Sick sense of humor? Visit Yahoo! TV's 
Comedy with an Edge to see what's on, when. 
http://tv.yahoo.com/collections/222



More information about the Python-ideas mailing list