Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Problem with list.remove() method (http://www.velocityreviews.com/forums/t954670-problem-with-list-remove-method.html)

Alvaro Combo 11-20-2012 01:56 PM

Problem with list.remove() method
 
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!!!

Any suggestion is MOST welcome.

Best Regards

ACombo

....And the code:

def remove_dup_5_10():
"""
Remove the duplicates of a given list. The original list MUST be kept.
"""

# Set the original list
lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]

# NEED to create a copy... See dicussion on Problem 5.6 and issue #2
cpy_lst = list(lst)

# Perform an infinite loop... explained later
i=0 # initialize the index
while i != len(cpy_lst):
if cpy_lst.count(cpy_lst[i]) != 1:
cpy_lst.remove(i)
else:
i += 1

print "The original List: ", lst
print "List with NO duplicates: ", cpy_lst

return True

Chris Angelico 11-20-2012 02:14 PM

Re: Problem with list.remove() method
 
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

Hope that helps!

ChrisA

Alvaro Combo 11-20-2012 02:37 PM

Re: Problem with list.remove() method
 
Dear Chris,

Thank you very much for you reply...
For a newcomer to Python the Hell is in the details... :-).

You are absolutely right... and I just used the index instead of the value.

Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.

Once again, Thank you and best regards

ACombo


Alvaro Combo 11-20-2012 02:37 PM

Re: Problem with list.remove() method
 
Dear Chris,

Thank you very much for you reply...
For a newcomer to Python the Hell is in the details... :-).

You are absolutely right... and I just used the index instead of the value.

Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.

Once again, Thank you and best regards

ACombo


Terry Reedy 11-20-2012 08:32 PM

Re: Problem with list.remove() method
 
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


Prasad, Ramit 11-20-2012 08:47 PM

RE: Problem with list.remove() method
 
Alvaro Combo 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!!!
>
> Any suggestion is MOST welcome.
>
> Best Regards
>
> ACombo
>
>...And the code:
>
> def remove_dup_5_10():
> """
> Remove the duplicates of a given list. The original list MUST be kept.
> """
>
> # Set the original list
> lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]
>
> # NEED to create a copy... See dicussion on Problem 5.6 and issue #2
> cpy_lst = list(lst)
>
> # Perform an infinite loop... explained later
> i=0 # initialize the index
> while i != len(cpy_lst):
> if cpy_lst.count(cpy_lst[i]) != 1:
> cpy_lst.remove(i)
> else:
> i += 1
>
> print "The original List: ", lst
> print "List with NO duplicates: ", cpy_lst
>
> return True
> --


Remove looks for the *value*not the *index* of the value.


>>> help([].remove)

Helpon built-in function remove:

remove(...)
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.

Change ` cpy_lst.remove(i)` to `cpy_lst.remove(cpy_lst[i])`.

~Ramit



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.

Chris Angelico 11-20-2012 08:48 PM

Re: Problem with list.remove() method
 
On Wed, Nov 21, 2012 at 1:37 AM, Alvaro Combo <alvaro.combo@gmail.com> wrote:
> Dear Chris,
>
> Thank you very much for you reply...
> For a newcomer to Python the Hell is in the details... :-).


You're most welcome! As Adam Savage of Mythbusters is fond of saying
(with an exaggerated accent), "It's all a learning experience".

> Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :-). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.


Yep. O(N^2) isn't inherently a problem, it just means that the
technique will scale poorly to large numbers of list elements. With
small lists, it'll be "fast enough" - for all intents and purposes,
the bulk of Python code executes in zero time, despite being in an oh
so slow interpreted language and using ridiculously inefficient (but
beautifully readable) code. That's the beauty of modern hardware :)

However, I do think that people should be aware when they're writing
non-scaleable code. That's not just algorithmic complexity; if you're
making a system that won't handle more than one request a second
(because, for instance, it uses seconds-since-1970 as a request
identifier), that's something worth being aware of, even if it's
unlikely ever to be a problem. Just know, so that if a problem ever
_does_ occur, you know where it is!

ChrisA

Dennis Lee Bieber 11-21-2012 12:08 AM

Re: Problem with list.remove() method
 
On Tue, 20 Nov 2012 05:56:27 -0800 (PST), Alvaro Combo
<alvaro.combo@gmail.com> declaimed the following in
gmane.comp.python.general:


> ...And the code:
>
> def remove_dup_5_10():
> """
> Remove the duplicates of a given list. The original list MUST be kept.
> """
>
> # Set the original list
> lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]
>
> # NEED to create a copy... See dicussion on Problem 5.6 and issue #2
> cpy_lst = list(lst)
>


Why copy the source list at the start and then...

> # Perform an infinite loop... explained later
> i=0 # initialize the index
> while i != len(cpy_lst):
> if cpy_lst.count(cpy_lst[i]) != 1:
> cpy_lst.remove(i)


.... remove elements from the copy...


> else:
> i += 1
>
> print "The original List: ", lst
> print "List with NO duplicates: ", cpy_lst
>


Wouldn't it be simpler to just add each NON-duplicate of the source
to the destination?

dest = []
for itm in source:
if itm not in dest:
dest.append(itm)

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/



All times are GMT. The time now is 02:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.