[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

map, index

map, index

Luis Quesada

3/28/2010 1:27:00 PM

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

Duncan Booth

3/28/2010 1:54:00 PM

Luis Quesada <l.quesada@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) ]

Luis Quesada

3/28/2010 2:34:00 PM

Duncan Booth wrote:
> Luis Quesada <l.quesada@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

Paul Rubin

3/28/2010 8:47:00 PM

Luis Quesada <l.quesada@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.

Luis Quesada

3/28/2010 10:15:00 PM

Paul Rubin wrote:
> Luis Quesada <l.quesada@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

Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
map, index

Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te

Il servizio è completamente GRATUITO!