newbie: working iwth list of tuples

Raymond Hettinger python at rcn.com
Sun Jan 29 02:24:45 EST 2006


[falcon]
> I am fairly new to Python (less than a week).  My goal is to write a
> small prototype of a database.  Rather than build it using the typical
> method where one provides selection, projection, aggregation, union,
> intersection, etc. functions, I would like to do it in a more
> 'functional' style (executing a database query by using map, reduce,
> filter, etc.  I am curious what the best way is to work with a list of
> tuples.  For example, if I have the following list:
> [('a',1),('a',1),('a',3),('b',1),('b',2),('c',2),('c',3),('c',4)]
> how do I apply a map to each item in each tuple in the list?  What if I
> want to filter based on both elements (the number AND the letter)?
>
> The most obvious solution is just to provide a function or a lambda
> expression whcih does what I want.  However, since this is, eventually,
> going to be a database, the user will decide at run time what functions
> and operations should be run on a 'table' (really a list of tuples) ...
> and the structure of the 'table' will obviously not be known until
> run-time.
>
> I didn't find any examples of using idioms from functional programming
> beyond basic explanation of reduce and map (I have a couple of Haskell
> and ML books, but I know those languages even less than Python).
>
> I hope you understand the problem.  Thanks in advance.

This is a somewhat advanced problem for your first week in Python.

Essentially, what you need to do is write parameterized helper
functions to pass to map(), filter(), and reduce().

First, look at a non-parameterized example.  Given a record, append a
new field that is the sum of fields 1 and 3:

    def z(record):
        sum = record[1] + record[3]
        return record + (sum,)

Apply it to the database with:

    result = map(z, database)

Now, make a parameterized version that allows the user specified fields
and operations:

    def pfunc(inputfields, operation):
        def z(record):
            newfield = operation(*[record[f] for f in inputfields])
            return record + (newfield,)
        return z

    z = pfunc((1, 3), operator.add)   # parameters specified by the
user at runtime

    result = map(z, database)

Parameterized filter, extract, and reduce functions can be handled in a
like manner.


Raymond




More information about the Python-list mailing list