string issue
Alan McIntyre
alan.mcintyre at esrgtech.com
Fri Feb 4 14:32:01 EST 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 ;)
More information about the Python-list
mailing list