![]() |
|
|
|
#11 |
|
Terry Reedy wrote:
> 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. > But shouldn't that have been lambda x: 'a' != x, which would have invoked the string (3.0, Unicode) comparison method? regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Steve Holden |
|
|
|
|
#12 |
|
Posts: n/a
|
En Thu, 26 Feb 2009 22:08:26 -0200, Peter Billam <>
escribió: > On 2009-02-26, 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? > > L2 = list(set(L)) > > works for me... For a very strange definition of "works": py> L = ['a','b','c','a','j','b','z','b','a'] py> L2 ['a', 'c', 'b', 'z', 'j'] py> L2 = list(set(L)) py> L2 ['a', 'c', 'b', 'z', 'j'] I still see an 'a', there are things missing, and the order is totally lost. -- Gabriel Genellina Gabriel Genellina |
|
|
|
#13 |
|
Posts: n/a
|
Peter Billam wrote:
> On 2009-02-26, 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? > > L2 = list(set(L)) > > works for me... I have to wonder for what value of "works" this works. The simplest problem is it doesn't remove all the "a"s. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Steve Holden |
|
|
|
#14 |
|
Posts: n/a
|
En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <>
escribió: > On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <> > wrote: >> On 2009-02-26, 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? >> >> L2 = list(set(L)) >> >> works for me... > > A. That doesn't by itself remove all the 'a's, although it does remove > all but 1 'a' > B. That also removes all but one instance of *everything* in the list, > not just 'a' > C. That is lossy in that it completely loses the ordering of the > original list I said the very same things! There are other issues too, like this only works with hashable objects, but I've chosen exactly the same remarks and exactly in the same order! What a coincidence... -- Gabriel Genellina Gabriel Genellina |
|
|
|
#15 |
|
Posts: n/a
|
On Thu, Feb 26, 2009 at 4:37 PM, Gabriel Genellina
<gagsl-> wrote: > En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <> > escribió: >> >> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <> >> wrote: >>> >>> On 2009-02-26, 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? >>> >>> L2 = list(set(L)) >>> >>> works for me... >> >> A. That doesn't by itself remove all the 'a's, although it does remove >> all but 1 'a' >> B. That also removes all but one instance of *everything* in the list, >> not just 'a' >> C. That is lossy in that it completely loses the ordering of the original >> list > > I said the very same things! > There are other issues too, like this only works with hashable objects, but > I've chosen exactly the same remarks and exactly in the same order! What a > coincidence... Indeed, USENET and mail<->news lags doth create interesting situations sometimes. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com Chris Rebert |
|
|
|
#16 |
|
Posts: n/a
|
On Feb 26, 3:05*am, Clarendon <jine...@hotmail.com> 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? > I would really appreciate your help. > > Thanks. while 'a' in L: L.remove('a') not the most efficient but it works odeits |
|
|
|
#17 |
|
Posts: n/a
|
On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:
> On Feb 26, 3:05Â*am, Clarendon <jine...@hotmail.com> 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? >> I would really appreciate your help. >> >> Thanks. > > while 'a' in L: > Â* L.remove('a') > > not the most efficient but it works "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M = number of 'a's in L], versus just N. And that's not even accounting for all the extra element movement done by .remove() Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com Chris Rebert |
|
|
|
#18 |
|
Posts: n/a
|
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote: [...] >> while 'a' in L: >> L.remove('a') >> >> not the most efficient but it works > > "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M > = number of 'a's in L], versus just N. And that's not even accounting > for all the extra element movement done by .remove() > Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in assessing performance order? regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Steve Holden |
|
|
|
#19 |
|
Posts: n/a
|
On Fri, Feb 27, 2009 at 5:10 AM, Steve Holden <> wrote:
> Chris Rebert wrote: >> On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote: > [...] >>> while 'a' in L: >>> Â* L.remove('a') >>> >>> not the most efficient but it works >> >> "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M >> = number of 'a's in L], versus just N. And that's not even accounting >> for all the extra element movement done by .remove() >> > Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in > assessing performance order? Obviously that equivalence is true, but in this case I'm emphasizing that it's even worse than that when constant factors are taken into account. Big-O is nice in the abstract, but in the real-world those constant factors can matter. In pure big-O, it is indeed O(M*N) vs. O(N) Including constant factors, the performance is roughly 2*M*N*X [X = overhead of remove()] vs. N, which makes the badness of the algorithm all the more apparent. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com Chris Rebert |
|
|
|
#20 |
|
Posts: n/a
|
Thank you very much for all your replies. I actually used the while
loop as the data is not large. But I was looking for a simpler, built in command, something like L.remove('a', all) or L.removeall('a'). Python has re.findall function, but why not removeall, so users have to make up long lines of expression? Clarendon |
|
![]() |
| 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 |