By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,520 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,520 IT Pros & Developers. It's quick & easy.

how to explain such codes, python's bug or mine?

P: n/a
>>> j = range(20)
print j [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] for k in j: if k <= 10:
j.remove(k)

print j [1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.

i think python do convert there codes to such style:
for (i = 0; i < len(j); i++)
k = j[i]
......

what do u think?
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

it is not python bug.
You refer the list j and remove the element in the same time, that is
the problem. Python dinamicaly goes to the next element with the same
index but apply it in the new list.

use this code instead:
j = range(20)
print j
L = [x for x in j if x > 10]
print L

Sincerely Yours,
Pujo

Jul 18 '05 #2

P: n/a
Jim
MaHahaXixi wrote:
j = range(20)
print j
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
for k in j:
if k <= 10:
j.remove(k)
print j

[1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.

i think python do convert there codes to such style:
for (i = 0; i < len(j); i++)
k = j[i]
......

what do u think?


I'm not quite sure of your question but with the second style you're not
attempting to change the original list but make a copy. That's perfectly
easy to do in Python as it is. The exampmle is a cautionary one about
changing the list on which you are iterating.

Jim
Jul 18 '05 #3

P: n/a
Hi

The second style can be used:
j = range(20)
print j
L = [x for x in j if x > 10]
j = L

There are another method such poping the item based on last index to 0:
for i in range(len(j)-1,0-1,-1):
if j[i]<=10:
j.pop(i)

print j

Pujo

Jul 18 '05 #4

P: n/a
yes. i think it does so.
it take me the whole afternoon to find out the bug (mine)
i change:
for i in range(len(j) -1, -1, -1):
d = j[i]
if d <= 10:
j.remove(d)

the real code is not so simple,so j[11:] will not work for me.
but, i think phthon could found that i remove the current element, why it
does not move the pointer automatically?
for python, i am a newbie, but i did not found the warning of such usage
from the python tutorial

<aj****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,

it is not python bug.
You refer the list j and remove the element in the same time, that is
the problem. Python dinamicaly goes to the next element with the same
index but apply it in the new list.

use this code instead:
j = range(20)
print j
L = [x for x in j if x > 10]
print L

Sincerely Yours,
Pujo

Jul 18 '05 #5

P: n/a
"MaHahaXixi" <en*********@hotmail.com> wrote:
for python, i am a newbie, but i did not found the warning of such usage
from the python tutorial


"4.2 for Statements"

"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." (followed by an example)

</F>

Jul 18 '05 #6

P: n/a
SORRY, my inattention
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma**************************************@pyth on.org...
"MaHahaXixi" <en*********@hotmail.com> wrote:
for python, i am a newbie, but i did not found the warning of such usage
from the python tutorial
"4.2 for Statements"

"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." (followed by an example)

</F>

Jul 18 '05 #7

P: n/a
yes. i understand now.
but i use another trick.
list is in vary size, so i do not wanna copy it.
"Jim" <jb*@cannedham.ee.ed.ac.uk> wrote in message
news:d3**********@scotsman.ed.ac.uk...
MaHahaXixi wrote:
>j = range(20)
>print j


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>for k in j:


if k <= 10:
j.remove(k)
>print j


[1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.

i think python do convert there codes to such style:
for (i = 0; i < len(j); i++)
k = j[i]
......

what do u think?


I'm not quite sure of your question but with the second style you're not
attempting to change the original list but make a copy. That's perfectly
easy to do in Python as it is. The exampmle is a cautionary one about
changing the list on which you are iterating.

Jim

Jul 18 '05 #8

P: n/a
yes, i use the 2th way
<aj****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi

The second style can be used:
j = range(20)
print j
L = [x for x in j if x > 10]
j = L

There are another method such poping the item based on last index to 0:
for i in range(len(j)-1,0-1,-1):
if j[i]<=10:
j.pop(i)

print j

Pujo

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.