Prev: AUTHENTIC DESIGNER HANDBAGS ACCESSORIES GREAT DISCOUNTS! WWW.VOGUELANDE.COM
Next: StringChain -- a data structure for managing large sequences ofchunks of bytes
From: Luis Quesada on 28 Mar 2010 09:26 Dear all, I am new to Python, so I apologize in advance if you find my questions naive (or if they have been already answered in this forum). 1. What is the most pythonic way of mapping a list where the value each element is mapped to depends on the index of the element. Is there a way of writing the following without using zip: map(lambda (id,v):id*v,zip(range(len(L)),L)) I wonder whether there is something like mapInd in Oz (http://www.mozart-oz.org/documentation/base/list.html) so that you can pass a binary function to map and refer to the index of the element in the body of the given function. It would be cool to be able to simply write: mapInd(lambda id,v:id*v,L) 2. Is there a higher order index function that lets you specify the condition to be met by the returned element. I mean something like: def my_index(f,L,default): for x in L: if f(x):return x return default Thanks in advance for your answer! Cheers, Luis
From: Duncan Booth on 28 Mar 2010 09:53 Luis Quesada <l.quesada(a)4c.ucc.ie> wrote: > Is there a way > of writing the following without using zip: > map(lambda (id,v):id*v,zip(range(len(L)),L)) [ id*v for id,v in enumerate(L) ]
From: Luis Quesada on 28 Mar 2010 10:34 Duncan Booth wrote: > Luis Quesada <l.quesada(a)4c.ucc.ie> wrote: > >> Is there a way >> of writing the following without using zip: >> map(lambda (id,v):id*v,zip(range(len(L)),L)) > > [ id*v for id,v in enumerate(L) ] Cool! Thanks! Cheers, Luis
From: Paul Rubin on 28 Mar 2010 16:46 Luis Quesada <l.quesada(a)4c.ucc.ie> writes: >> [ id*v for id,v in enumerate(L) ] > Cool! Thanks! If you really want to write that in pointfree style (untested): import itertools, operator ... itertools.starmap(operator.mul, enumerate(L)) For your other question, you could probably do something ugly with ifilter, but it's not worth it. For the style of programming you're interested in, you should read the docs for the itertools module. Python culture in general tends to not be that fond of heavy use of HOF's, since it's very easy to make mistakes with its iterator protocol and with its dynamic type system.
From: Luis Quesada on 28 Mar 2010 18:14
Paul Rubin wrote: > Luis Quesada <l.quesada(a)4c.ucc.ie> writes: >>> [ id*v for id,v in enumerate(L) ] >> Cool! Thanks! > > If you really want to write that in pointfree style (untested): > > import itertools, operator > ... > itertools.starmap(operator.mul, enumerate(L)) > > For your other question, you could probably do something ugly > with ifilter, but it's not worth it. > > For the style of programming you're interested in, you should read the > docs for the itertools module. Python culture in general tends to > not be that fond of heavy use of HOF's, since it's very easy to make > mistakes with its iterator protocol and with its dynamic type system. Thanks a lot for the pointer. Python is rather cool indeed. Cheers, Luis |