473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

???? i can`t understand it

>>> a=[1,2,3,4,5]
for b in a: .... a.remove(b)
.... a [2, 4]


Jul 18 '05 #1
3 1482
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

Jul 18 '05 #2
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
Jul 18 '05 #3
Smille Purusa <Sm****@mail.com> 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 <rm***@pobox.com> | 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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
7
by: Robert Allan Schwartz | last post by:
Why do I get a syntax error below? I don't see why volatile works but unsigned does not work. I'm not looking for an answer of the form, "Because the Standard says so", or "Because the C++...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
14
by: jlemay | last post by:
if you look at http://www.emrose.net/contact1.html the last field is a textarea field and I can't make it align with the other text fields. it's pretty close, but this comes from trial and...
2
by: DC | last post by:
Hi, I need to asynchronously read from a network (TCP) stream, and I am having trouble with retrieving whole blocks; I get a break in the data block every 1460 bytes which relates to network...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
24
by: Richard Aubin | last post by:
I'm really new to vb.net programming and programming in general. I would like to teach myself on how to program effectively and I have the financial and time resources to do so. Can I anyone...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.