sort and delete ?

Mikael Olofsson mikael at isy.liu.se
Tue May 30 13:09:20 CEST 2000


On 30-May-00 matt wrote:
 >  Is there a more "pythonic" way to do the following, which, before I make a
 >  mess
 >  is, suppossed to assess whether to throw elements out of a list and then
 >  actually do so.  I use a rather strange, but database-like, list.
 >  
 >  a = [['egg', [1, 'ace', 3]], ['apple', [1, 'two', 3]], ['pear', [4,
 >  'three',
 >  6]], ['orange', [5, 'two', 1]]]
 >  
 >  x=[]
 >  for i in a:
 >      if i[0][1] in ('ace','three'):
 >              x.append(i)

First of all, I think you meant i[1][1] above. I am an addict under the
lambda-forms, so I would do it using filter:

x = filter(lambda i:i[1][1] in ('ace','three'),a)

Cheers,

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    30-May-00
Time:    13:04:55

This message was sent by XF-Mail.
-----------------------------------------------------------------------




More information about the Python-list mailing list