newbie: working iwth list of tuples

John Bauman john at baumanfamily.com
Sun Jan 29 09:07:39 CET 2006


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message 
news:7xoe1vtu68.fsf at ruckus.brouhaha.com...
> "falcon" <shahbazc at gmail.com> writes:
>> I forgot to add that I passing a tuple of functions to the reduce
>> function but apparently that is not allowed.  My guess was that a tuple
>> made up of individual (simple) functions might be easier to manipulate
>> programatically than a function which has to know the structure of a
>> list.
>
> Python really doesn't support this style all that well.  Try Haskell.
> See also the SICP book, which uses Scheme.
>
>> (My test code)
>> x=[('a',1),('a',1),('a',3),('b',1),('b',2),('c',2),('c',3),('c',4)]
>> reduce((lambda x,y: x+y,lambda x,y: x+y),x)
>
> I can't even tell what you're trying to do there.
I think that this is what he wants.
def zipfunc(fun):
    def runfun(*args):
        return tuple(x(*y) for x,y in zip(fun,zip(*args)))
    return runfun

Use this as:
x=[('a',1),('a',1),('a',3),('b',1),('b',2),('c',2),('c',3),('c',4)]
reduce(zipfunc((lambda x,y: x+y,lambda x,y: x+y)),x)
I think he wants the apply the first function in the tuple to the first 
element in every tuple, and likewise the second, third, etc. 





More information about the Python-list mailing list