[Tutor] Hmmm...

Pedro Diaz Jimenez pdiaz88@terra.es
Sat, 10 Mar 2001 19:49:32 +0100


Didn't read all the replies, so sorry if this response is a repetition

lambda attack to the problem:
>>> a=['pera','manzana','limon','naranja']
>>> filter( lambda x:x[len(x)-1]=='a', a )
['pera', 'manzana', 'naranja']
>>>                                                

Cheers
Pedro

On Saturday 10 March 2001 03:38, Danny Yoo wrote:
> On Fri, 9 Mar 2001, Britt Green wrote:
> > import string
> >
> > theFruits = ['apple', 'banana', 'pineapple', 'pear', 'orange']
> >
> > chosen = []
> >
> > for stuff in theFruits:
> >     if stuff.endswith('e'):
> >         chosen.append(stuff)
> >
> >     print chosen
> >
> > When run, only the elements of theFruits that end with an 'e' will get
> > added to the list of chosen. Is there a better way to do this?
>
> Here's another wacky way to do that:
>
> ###
> def endsWithE(word):
>     return word.endswith('e')
>
> theFruits = ['apple', 'banana', 'pineapple', 'pear', 'orange']
> chosen = filter(endsWithE, theFruits)
> print chosen
> ###
>
> which tells Python: "Ok, let's filter out only theFruits that
> endsWithE()."  This is called the "functional" approach, because all we're
> doing is function calls and passing functions around.  It's a lot of fun.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor