473,396 Members | 1,886 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,396 software developers and data experts.

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

>>> 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
8 1258
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Bendzick | last post by:
Is there a simple way in python to read a keyboard scan code? I'm working on a shell script that interfaces with a proprietary keyboard device (extra buttons) and need to be able to distinguish...
1
by: Yong Wang | last post by:
Hi, All: We have a network management system written in C++, MysQL, and Hp SNMP. It works in Solaris command line. When I wrote a similar python codes which call compiled C++ and mysql codes in...
29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
2
by: Phil Green | last post by:
Hi, I have been creating a CD-based site which works exactly how I want in IE5. I decided that I should test it in Mozilla Firefox and Opera. Unfortunately, a lot of the scrips don't work as...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
99
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
29
by: Mike Meyer | last post by:
After spending time I should have been sleeping working on it, the try python site is much more functional. It now allows statements, including multi-line statements and expressions. You can't...
27
by: hacker1017 | last post by:
im just asking out of curiosity.
3
by: sapsi | last post by:
Hello, I'm not sure if this the correct list but here goes (and sorry for the noise). I've been attempting to wrap python around libhdfs. So far so good (i've attached the SWIG template at the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.