473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string issue

rbt
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)

Someone tell me I'm going crazy ;)
Jul 18 '05 #1
28 1535
I think it's because you're modifying the list as you're iterating over
it. Try this:

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']
ips_new = []
for ip in ips:
if '255' not in ip:
ips_new.append( ip)

print ips_new

Or this:

ips_new = [ip for ip in ips if '255' not in ip]
print ips_new
Hope this helps,
Alan McIntyre
http://www.esrgtech.com

rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)

Someone tell me I'm going crazy ;)

Jul 18 '05 #2
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)

Someone tell me I'm going crazy ;)


You're going crazy. ;)

py> ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']
py> for ip in ips:
.... # debugging statement:
.... print "Looking at", ip
.... if '255' in ip:
.... try:
.... print "Removing", ip
.... ips.remove(ip)
.... except Exception, e:
.... print e
....
Looking at 255.255.255.255
Removing 255.255.255.255
Looking at 198.82.247.98
Looking at 127.0.0.1
Looking at 255.0.0.0
Removing 255.0.0.0
Looking at 128.173.255.34
Removing 128.173.255.34

Notice how elements of your list are being skipped. The problem is that
you're modifying a list while you iterate over it.

Why don't you try a list comprehension:

py> ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']
py> [ip for ip in ips if '255' not in ip]
['128.173.120.79 ', '198.82.247.98' , '127.0.0.1']

Steve
Jul 18 '05 #3
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)

Someone tell me I'm going crazy ;)


You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:

for ip in ips[:]:
...

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #4
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <rb*@athop1.ath .vt.edu> wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)


You're gong crazy:
ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' , .... '127.0.0.1', '255.0.0.0', '255', '128.173.255.34 '] for ip in ips: .... if '255' in ip: print ip
....
255.255.255.255
255.0.0.0
255
128.173.255.34

The problem is that you're operating in-place on an array while it's
being iterated over. Since the iterator is only created once, you're
can't change the array while you're iterating over it. Instead, try a
list comprehension:
ips = [ip for ip in ips if '255' not in ip]
ips

['128.173.120.79 ', '198.82.247.98' , '127.0.0.1']

Peace
Bill Mill
bill.mill at gmail.com
Jul 18 '05 #5
rbt
Thanks guys... list comprehension it is!

Bill Mill wrote:
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <rb*@athop1.ath .vt.edu> wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1' , '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep( 5)

You're gong crazy:

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
... '127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']
for ip in ips:
... if '255' in ip: print ip
...
255.255.255.255
255.0.0.0
255
128.173.255.34

The problem is that you're operating in-place on an array while it's
being iterated over. Since the iterator is only created once, you're
can't change the array while you're iterating over it. Instead, try a
list comprehension:

ips = [ip for ip in ips if '255' not in ip]
ips


['128.173.120.79 ', '198.82.247.98' , '127.0.0.1']

Peace
Bill Mill
bill.mill at gmail.com

Jul 18 '05 #6
Wow, that's cool; I'd never seen that before. :) Thanks, Steve..

Steve Holden wrote:
You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:

for ip in ips[:]:
...

regards
Steve

Jul 18 '05 #7
rbt
Steve Holden wrote:
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is
something wrong with this code. Element 5 of this list says it doesn't
contain the string 255, when that's *ALL* it contains... why would it
think that???

import time

ips = ['255.255.255.25 5', '128.173.120.79 ', '198.82.247.98' ,
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34 ']

for ip in ips:
if '255' in ip:
try:
print "Removing", ip
ips.remove(ip)
except Exception, e:
print e

print ips
time.sleep(5)

Someone tell me I'm going crazy ;)

You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:

for ip in ips[:]:
...

regards
Steve


Very neat. That's a trick that everyone should know about. I vote it
goes in Dr. Dobbs newsletter.
Jul 18 '05 #8
Steve Holden wrote:
You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:

for ip in ips[:]:
...


Also worth noting, you can write this like:

for ip in list(ips):
...

if you're afraid of smiley-faces [:] in your code. ;)

Steve
Jul 18 '05 #9
rbt
Alan McIntyre wrote:
I think it's because you're modifying the list as you're iterating over


In this case then, shouldn't my 'except Exception' raise an error or
warning like:

"Hey, stupid, you can't iterate and object and change it at the same time!"

Or, perhaps something similar?
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8193
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
2249
by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
17
4676
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
32
14915
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
8
6209
by: Ottar | last post by:
I have a few numeric fields, and when I update i get the error: "Input string was not in a correct format". Next line:" System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2178925" This is beacuse some of my TextBoxes are empty (no value). If I enter a value it is OK. I need to use Null values to indicate that the field is not fileld in.
47
5025
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
94
4788
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
1
2129
by: jwf | last post by:
This question continues on from a previous post "DATE to string" but I think it deserves a new thread. In my previous post I was trying to convert a DATE to string in a NON MFC C++ application which (with a little help) was achieved with the following code: ****************** // Variables DATE MailDateTime;
14
4098
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
1
3661
by: kru | last post by:
Hi All, Simple issue I cannot figure out. I have a multiline textbox, I need to convert the string contents of this textbox into a single line string which I currently write to a textfile. I've attempted to cleanse the contents of the textbox by removing ASCII chars 0-31 which includes carriage returns and replacing all
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.