Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ???? i can`t understand it

Reply
Thread Tools

???? i can`t understand it

 
 
Enrique
Guest
Posts: n/a
 
      08-08-2003
>>> a=[1,2,3,4,5]
>>> for b in a:

.... a.remove(b)
....
>>> a

[2, 4]
>>>


 
Reply With Quote
 
 
 
 
David C. Fox
Guest
Posts: n/a
 
      08-08-2003
Enrique wrote:
> >>> a=[1,2,3,4,5]
> >>> for b in a:

> ... a.remove(b)
> ...
> >>> a

> [2, 4]
> >>>


As others have noted, modifying a sequence can have strange effects on
iterators.

Presumably, the example above is simplified (since if you really wanted
to remove all elements in a, you would just say a = []). If you want to
selectively remove elements in a, you should have a look at the built-in
function filter. For example, to remove elements < 0:

a = [1,5,7, -4, 2, -10]
a = filter(lambda x: x >= 0, a)

David

 
Reply With Quote
 
 
 
 
Smille Purusa
Guest
Posts: n/a
 
      08-11-2003
Enrique wrote:

>>>> a=[1,2,3,4,5]
>>>> for b in a:

> ... a.remove(b)
> ...
>>>> a

> [2, 4]
>>>>

Very interesting result but reasonable. If the underlying interpreter uses
a reference or pointer for the job like this:

# psudo codes for python
for(ptr = a.first(); ptr.is_valid(); ++ptr)
{
update('b', value(ptr))
call_method('a', 'remove', get_alue('b'))
}

ptr may be just an index. So at the first iteration, the first element, '1',
is removed from a. The next time ptr=1, but a has been changed to [2,3,4,5],
so '3' is removed, and so on,


Smille
 
Reply With Quote
 
Robin Munn
Guest
Posts: n/a
 
      08-12-2003
Smille Purusa <(E-Mail Removed)> wrote:
> Enrique wrote:
>
>>>>> a=[1,2,3,4,5]
>>>>> for b in a:

>> ... a.remove(b)
>> ...
>>>>> a

>> [2, 4]
>>>>>

> Very interesting result but reasonable. If the underlying interpreter uses
> a reference or pointer for the job like this:
>
> # psudo codes for python
> for(ptr = a.first(); ptr.is_valid(); ++ptr)
> {
> update('b', value(ptr))
> call_method('a', 'remove', get_alue('b'))
> }
>
> ptr may be just an index. So at the first iteration, the first element, '1',
> is removed from a. The next time ptr=1, but a has been changed to [2,3,4,5],
> so '3' is removed, and so on,


This is exactly correct. From the Python reference manual, describing
for loops:

Warning: There is a subtlety when the sequence is being modified by
the loop (this can only occur for mutable sequences, i.e. lists). An
internal counter is used to keep track of which item is used next,
and this is incremented on each iteration. When this counter has
reached the length of the sequence the loop terminates. This means
that if the suite deletes the current (or a previous) item from the
sequence, the next item will be skipped (since it gets the index of
the current item which has already been treated). Likewise, if the
suite inserts an item in the sequence before the current item, the
current item will be treated again the next time through the loop.
This can lead to nasty bugs that can be avoided by making a
temporary copy using a slice of the whole sequence, e.g.,

for x in a[:]:
if x < 0: a.remove(x)

You can read the whole thing at:
http://www.python.org/doc/current/ref/for.html

--
Robin Munn <(E-Mail Removed)> | http://www.rmunn.com/ | PGP key 0x6AFB6838
-----------------------------+-----------------------+----------------------
"Remember, when it comes to commercial TV, the program is not the product.
YOU are the product, and the advertiser is the customer." - Mark W. Schumann
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an error on multi-source, but I can't understand... Neil VHDL 11 03-11-2010 03:15 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
understand Mozilla Thunderbird files... Joh Firefox 6 12-11-2004 11:46 PM
Configure & understand radius Julien Cisco 0 06-07-2004 09:12 AM
how to read and understand long written VHDL code? walala VHDL 6 09-03-2003 07:49 PM



Advertisments