Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33655

Re: Problem with list.remove() method

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '21,': 0.07; 'python': 0.09; 'list...': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'section,': 0.09; 'subject:method': 0.09; 'terry': 0.09; 'assume': 0.11; 'slightly': 0.15; 'doc,': 0.16; 'duplicates': 0.16; 'element.': 0.16; 'index.': 0.16; 'itertools': 0.16; 'losing': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recipe': 0.16; 'reedy': 0.16; 'set()': 0.16; 'subject:Problem': 0.16; 'task.': 0.16; 'wingide': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'duplicate': 0.17; 'element': 0.17; 'odd': 0.17; 'jan': 0.18; 'code,': 0.18; 'written': 0.20; 'all,': 0.21; 'trying': 0.21; 'fine,': 0.22; 'elements': 0.23; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'change,': 0.27; 'set.': 0.27; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; 'points': 0.29; "i'm": 0.29; 'basic': 0.30; 'function': 0.30; 'error': 0.30; 'running': 0.32; "aren't": 0.33; 'instead,': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'nov': 0.35; 'doing': 0.35; "won't": 0.35; 'something': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'keeps': 0.37; 'uses': 0.37; 'maintaining': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'remove': 0.61; "you've": 0.61; 'first': 0.61; "you'll": 0.62; 'relatively': 0.62; 'alvaro': 0.84; 'combo': 0.84; 'exercise.': 0.84; 'order:': 0.84; 'received:fios.verizon.net': 0.84; 'sighted': 0.84; 'approach.': 0.91; 'inefficient': 0.91; 'technique': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Problem with list.remove() method
Date Tue, 20 Nov 2012 15:32:21 -0500
References <b610616c-c26d-44e1-84c6-fa922a8e3f75@googlegroups.com> <CAPTjJmqqc+_BaoHVouO-9RBxehtfNY=aWSUDK6e0isbYe0C_RQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2
In-Reply-To <CAPTjJmqqc+_BaoHVouO-9RBxehtfNY=aWSUDK6e0isbYe0C_RQ@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.90.1353443565.29569.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353443565 news.xs4all.nl 6984 [2001:888:2000:d::a6]:49042
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33655

Show key headers only | View raw


On 11/20/2012 9:14 AM, Chris Angelico wrote:
> On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo <alvaro.combo@gmail.com> wrote:
>> Hi All,
>>
>> I'm relatively new to Python... but I have found something I cannot explain... and  I'm sure you can help me.
>>
>> I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task.
>>
>> I'm using WingIDE as editor/debugger and have Python 2.7.3.
>>
>> When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!!
>
> Several points here. You've written a beautiful O(N^2) duplicates
> remover... Python has a really fast way of doing it, if you don't mind
> losing order:
>
> cpy_lst = list(set(lst))
>
> But let's assume you're doing this for the exercise. Your technique is
> fine, if inefficient on large lists, but the remove() method looks for
> the first occurrence of an element by its value - what you want is:
>
> del cpy_lst[i]
>
> which will remove one element by index.
>
> With that change, you'll have a slightly odd duplicate remover that
> keeps the *last* of any given element. That's rather unusual. Instead,
> you may want to consider maintaining a set of "items I've already
> seen", and keeping all elements that aren't in that set. I won't give
> you all the code, but here's the basic set operations:
>
> sighted = set()
> sighted.add(some_element)
> if some_element in sighted:   # condition is True if you've already
> seen this element

The itertools doc, in the last section, has a recipe for this problem 
that uses the above approach.


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 05:56 -0800
  Re: Problem with list.remove() method Chris Angelico <rosuav@gmail.com> - 2012-11-21 01:14 +1100
    Re: Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 06:37 -0800
      Re: Problem with list.remove() method Chris Angelico <rosuav@gmail.com> - 2012-11-21 07:48 +1100
    Re: Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 06:37 -0800
  Re: Problem with list.remove() method Terry Reedy <tjreedy@udel.edu> - 2012-11-20 15:32 -0500
  RE: Problem with list.remove() method "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-20 20:47 +0000
  Re: Problem with list.remove() method Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-20 19:08 -0500

csiph-web