![]() |
|
|
|
#1 |
|
Hi. This must be a simple command but I just can't find it in the
Phthon manual. How do I delete all items with a certain condition from a list? For instance: L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? I would really appreciate your help. Thanks. Clarendon |
|
|
|
|
#2 |
|
Posts: n/a
|
On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:
> Hi. This must be a simple command but I just can't find it in the > Phthon manual. How do I delete all items with a certain condition from > a list? For instance: > > L=['a', 'b', 'c', 'a'] > > I want to delete all 'a's from the list. > But if L.remove('a') only deletes the first 'a'. > > How do you delete all 'a's? There are several ways. I'd go with a list comprehension: L = [i for i in L if i != 'a'] Or to modify the list in-place: L[:] = [i for i in L if i != 'a'] Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com Chris Rebert |
|
|
|
#3 |
|
Posts: n/a
|
Clarendon a écrit :
> Hi. This must be a simple command but I just can't find it in the > Phthon manual. How do I delete all items with a certain condition from > a list? For instance: > > L=['a', 'b', 'c', 'a'] > > I want to delete all 'a's from the list. > But if L.remove('a') only deletes the first 'a'. > > How do you delete all 'a's? L[:] = [item for item in L if item != 'a'] Bruno Desthuilliers |
|
|
|
#4 |
|
Posts: n/a
|
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote: .... >> L=['a', 'b', 'c', 'a'] >> >> I want to delete all 'a's from the list. >> But if L.remove('a') only deletes the first 'a'. >> >> How do you delete all 'a's? > > There are several ways. I'd go with a list comprehension: and for a couple other ways while 'a' in L : L.remove('a') L = filter('a'.__ne__,L) Boris Borcic |
|
|
|
#5 |
|
Posts: n/a
|
En Thu, 26 Feb 2009 11:00:30 -0200, Boris Borcic <>
escribió: > Chris Rebert wrote: >> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote: > ... >>> L=['a', 'b', 'c', 'a'] >>> >>> I want to delete all 'a's from the list. >>> But if L.remove('a') only deletes the first 'a'. >>> >>> How do you delete all 'a's? >> There are several ways. I'd go with a list comprehension: > > and for a couple other ways > > while 'a' in L : L.remove('a') This is probably the worst way, takes cuadratic time (and two searches per loop). > L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ so a more generic answer would be: from functools import partial from operator import ne L = filter(partial(ne, 'a'), L) Of course, this is only relevant if L is large and there are many duplicates. In other cases I'd stick with the list comprehension as posted by Chris Rebert. -- Gabriel Genellina Gabriel Genellina |
|
|
|
#6 |
|
Posts: n/a
|
>>
>> > L = filter('a'.__ne__,L) >> >> And this is probably the fastest. But not all types define >> __ne__ so a >> more generic answer would be: >> >> from functools import partial >> from operator import ne >> L = filter(partial(ne, 'a'), L) >> And don't forget this "traditional" solution: >>> L=['a', 'b', 'c', 'a'] >>> filter(lambda arg: arg != 'a', L) ['b', 'c'] -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.11850 http://www.pctools.com/en/spyware-doctor-antivirus/ John Posner |
|
|
|
#7 |
|
Posts: n/a
|
>>
>> from functools import partial >> from operator import ne >> L = filter(partial(ne, 'a'), L) >> I learned about functools.partial only recently (on this list). functools is implemented in C, not Python, so I couldn't answer this question for myself: Is functools.partial completely equivalent to a manually-coded closure? Help, please. -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.11850 http://www.pctools.com/en/spyware-doctor-antivirus/ John Posner |
|
|
|
#8 |
|
Posts: n/a
|
Gabriel Genellina wrote:
> >> L = filter('a'.__ne__,L) > > And this is probably the fastest. But not all types define __ne__ In Py3, all classes inherit .__ne__ from 'object'. Terry Reedy |
|
|
|
#9 |
|
Posts: n/a
|
Terry Reedy wrote:
> Gabriel Genellina wrote: > >> >>> L = filter('a'.__ne__,L) >> >> And this is probably the fastest. But not all types define __ne__ > > In Py3, all classes inherit .__ne__ from 'object'. > Isn't that inherited method just an identity comparison? However in this particular case the main point is that str *does* implement __ne__. So as long as that built-in method doesn't call the other operand's __ne__ there should be no problem. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Steve Holden |
|
|
|
#10 |
|
Posts: n/a
|
Steve Holden wrote:
> Terry Reedy wrote: >> Gabriel Genellina wrote: >> >>>> L = filter('a'.__ne__,L) >>> And this is probably the fastest. But not all types define __ne__ >> In Py3, all classes inherit .__ne__ from 'object'. >> > Isn't that inherited method just an identity comparison? Yes. And so is, by default, the snipped slower proposed alternative of lambda x: a!= x. > However in this particular case the main point is that str *does* > implement __ne__. So as long as that built-in method doesn't call the > other operand's __ne__ there should be no problem. > > regards > Steve Terry Reedy |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can't delete file or folder? | nork | Software | 1 | 11-02-2009 11:52 AM |
| asp.net : custom code to delete data from Grid view (Database) | sara_23apr | Software | 1 | 06-19-2007 01:08 PM |
| Update and Delete From Gridview | Usman | Software | 0 | 11-01-2006 10:05 AM |
| How do I: Correctly deny the delete permission? | sundevilkid85 | Software | 0 | 05-08-2006 09:49 PM |
| Latest Tech Fiasco... | Ghost | A+ Certification | 30 | 01-09-2004 12:15 PM |