
July 18th, 2005, 08:48 AM
| | | python said : "1, 2, 3, 6, 7, manbo !"
At the python2.3.2 prompt
[color=blue][color=green][color=darkred]
>>> a = range(8)
>>> for b in a:[/color][/color][/color]
if b == 4:
a.remove(b)
else:
print b
0
1
2
3
6
7
Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)
RodrigoB | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
(corrected the identation)
[color=blue]
> At the python2.3.2 prompt
>[color=green][color=darkred]
> >>> a = range(8)
> >>> for b in a:[/color][/color]
> ...........if b == 4:
> ................a.remove(b)
> ...........else:
> ................print b
>
>
> 0
> 1
> 2
> 3
> 6
> 7
>
>
> Why 5 does not appear ? (this was the source of a deep bug in a 4000+
> lines networked program...)
>
> RodrigoB
>[/color] | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
On Wed, 21 Jan 2004 17:53:19 -0300, - wrote:[color=blue]
> Why 5 does not appear ? (this was the source of a deep bug in a 4000+
> lines networked program...)[/color]
Curious:
[color=blue][color=green][color=darkred]
>>> a = range(8)
>>> for b in a:[/color][/color][/color]
... if ( b == 4 ):
... a.remove(b)
... print ( b, a )
...
(0, [0, 1, 2, 3, 4, 5, 6, 7])
(1, [0, 1, 2, 3, 4, 5, 6, 7])
(2, [0, 1, 2, 3, 4, 5, 6, 7])
(3, [0, 1, 2, 3, 4, 5, 6, 7])
(4, [0, 1, 2, 3, 5, 6, 7])
(6, [0, 1, 2, 3, 5, 6, 7])
(7, [0, 1, 2, 3, 5, 6, 7])
It seems that, having removed the current item in the list, the index of
the "for" loop hasn't changed. The next iteration increments that
index, even though the list itself has become shorter.
--
\ "You know I could rent you out as a decoy for duck hunters?" |
`\ -- Groucho Marx |
_o__) |
Ben Finney <http://bignose.squidly.org/> | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
- wrote:
[color=blue]
> Why 5 does not appear ? (this was the source of a deep bug in a 4000+
> lines networked program...)[/color]
You are iterating over a mutable sequence while you are mutating it.
That is a big no-no.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Life is painting a picture, not doing a sum.
-- Oliver Wendell Holmes, Jr. | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
On Wed, 21 Jan 2004 19:03:44 -0800, Erik Max Francis wrote:[color=blue]
> You are iterating over a mutable sequence while you are mutating it.
> That is a big no-no.[/color]
Got a pointer to somewhere that says so? Or at least describes the
expected behaviour for iteration over mutable types?
--
\ "Love is the triumph of imagination over intelligence." -- |
`\ Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/> | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
Ben Finney wrote:
[color=blue]
> Got a pointer to somewhere that says so? Or at least describes the
> expected behaviour for iteration over mutable types?[/color]
Tutorial, section 4.2: http://www.python.org/doc/current/tu...00000000000000
"It is not safe to modify the sequence being iterated over in the loop
(this can only happen for mutable sequence types, such
as lists). If you need to modify the list you are iterating over (for
example, to duplicate selected items) you must iterate over
a copy."
This is actually not specific to Python; in C++'s Standard Library, for
instance, there are complicated ramifications of modifying a container's
contents while you're actively maintaining and using an iterator over
it.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Isn't the best defense always a good attack?
-- Ovid | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
Erik Max Francis wrote in message <400F3D90.2176087B@alcyone.com>...[color=blue]
>- wrote:
>[color=green]
>> Why 5 does not appear ? (this was the source of a deep bug in a 4000+
>> lines networked program...)[/color]
>
>You are iterating over a mutable sequence while you are mutating it.
>That is a big no-no.
>[/color]
Try this instead:
a = [i for i in range(8) if not i == 4]
--
Francis Avila | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
Hello Rodrigo,[color=blue][color=green][color=darkred]
> >>> a = range(8)
> >>> for b in a:[/color][/color]
> if b == 4:
> a.remove(b)
> else:
> print b[/color]
try:
a = range(8)
for b in list(a): # Iterate over a shallow copy
if b == 4:
a.remove(b)
else:
print b
0
1
2
3
5
6
7
HTH.
Miki | 
July 18th, 2005, 08:48 AM
| | | Re: python said : "1, 2, 3, 6, 7, manbo !"
Erik Max Francis wrote:[color=blue]
> - wrote:
>
>[color=green]
>>Why 5 does not appear ? (this was the source of a deep bug in a 4000+
>>lines networked program...)[/color]
>
>
> You are iterating over a mutable sequence while you are mutating it.
> That is a big no-no.[/color]
This can sometimes be done safely by iterating backwards:
n = len(seq)
for i in range(n-1, -1, -1):
... | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,338 network members.
|