[Tutor] selecting specific items from a list

Benoit Dupire bdupire@seatech.fau.edu
Tue, 08 May 2001 12:32:20 -0400


alan.gauld@bt.com wrote:

> > Let's say I have the following list:
> >
> > [(3,3),(2,0),(5,5), (1,1), (4,5), (a,a), (a,e), (b,b), (c,d)]
> >
> > How can I tell the computer to return to me only those items
> > in the list for
> > which x==y.  That is, [(3,3), (5,5), (1,1), (a,a), (b,b)].
>
> Thats exactly what reduce() is for.

you mean filter(), uh?

>
>
> def sames(t): return t[0] == t[1]  # test an element
> newlist = reduce(sames, juliettelist) #use the test as an argument

>
>
> Take a look at my Functional Programming for an intro to these
> types of functions:
>
> http://www.crosswinds.net/~agauld/tutfctnl.htm
>
> Alan G
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?