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

2005-04-13 Thread 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[i]
   ..



what do u think?


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-13 Thread 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[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

<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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
>


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-13 Thread MaHahaXixi
SORRY, my inattention
"Fredrik Lundh" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> "MaHahaXixi" <[EMAIL PROTECTED]> 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)
>
> 
>
>
>


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-13 Thread MaHahaXixi
yes. i understand now.
but i use another trick.
list is in vary size, so i do not wanna copy it.
"Jim" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-13 Thread MaHahaXixi
yes, i use the 2th way
<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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
>


-- 
http://mail.python.org/mailman/listinfo/python-list