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

M

MaHahaXixi

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
......



what do u think?
 
A

ajikoe

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
 
J

Jim

MaHahaXixi said:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

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



[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
......

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
 
A

ajikoe

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<=10:
j.pop(i)

print j

Pujo
 
M

MaHahaXixi

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
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
 
F

Fredrik Lundh

MaHahaXixi said:
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>
 
M

MaHahaXixi

SORRY, my inattention
Fredrik Lundh said:
"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>
 
M

MaHahaXixi

yes. i understand now.
but i use another trick.
list is in vary size, so i do not wanna copy it.
Jim said:
MaHahaXixi said:
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)



[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
......

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top