Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string issue

Reply
Thread Tools

string issue

 
 
Jeremy Bowers
Guest
Posts: n/a
 
      02-04-2005
On Fri, 04 Feb 2005 14:49:43 -0500, rbt wrote:

> 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!"


But you can do that, so why should it?

Certain operations are dangerous, but, well, it's impossible to enumerate
all the dangerous things and it's better to not give people false
assurances when they find one of them that slipped by.

One idiom that does what you want, though it doesn't always work for all
situations, is to iterate over the list backwards. If you're only removing
a few items from a short list, that can be faster than building a new one.
However, removing items gets to be n^2 pretty fast, so it can be better to
build a new list, which the list comprehensions make easy:

newList = [x for x in oldList if myCondition(x)]

 
Reply With Quote
 
 
 
 
rbt
Guest
Posts: n/a
 
      02-04-2005
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.255', '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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      02-04-2005
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.255', '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/
 
Reply With Quote
 
Alan McIntyre
Guest
Posts: n/a
 
      02-04-2005
I think it's because you're modifying the list as you're iterating over
it. Try this:

import time

ips = ['255.255.255.255', '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.255', '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

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      02-04-2005
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.255', '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.255', '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.255', '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
 
Reply With Quote
 
Bill Mill
Guest
Posts: n/a
 
      02-04-2005
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <(E-Mail Removed)> 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.255', '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.255', '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
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      02-04-2005
Thanks guys... list comprehension it is!

Bill Mill wrote:
> On Fri, 04 Feb 2005 14:23:36 -0500, rbt <(E-Mail Removed)> 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.255', '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.255', '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

 
Reply With Quote
 
Alan McIntyre
Guest
Posts: n/a
 
      02-04-2005
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

 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      02-04-2005
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.255', '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.
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      02-04-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments