字符串问题 [英] string issue

查看:69
本文介绍了字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要么我疯了又要错过这里显而易见的东西,或者这个代码有一些错误

错误。这个列表的元素5表示它不包含

字符串255,当它'* ALL *它包含...为什么它会认为???


导入时间


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在ips中:

如果在IP中''255'':

尝试:

打印"删除",ip

ips.remove(ip)

除了Exception,e:

打印e


打印ips

time.sleep(5)


有人告诉我,我要疯了;)

解决方案

我认为这是因为你正在修改列表,因为你正在迭代

它。试试这个:


导入时间


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 = []
$ i b $ b for ip in ips:

如果''255''不在ip:

ips_new.append(ip )


打印ips_new


或者这个:


ips_new = [ip for ip in ips如果''255''不在ip中]

打印ips_new

希望这会有所帮助,

Alan McIntyre
http://www.esrgtech.com


rbt写道:

要么我疯了又要错过这里显而易见的或者这个代码有什么错误。这个列表的元素5表示它不包含
字符串255,当它'* ALL *它包含...为什么它会认为???

import时间

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中的ip:
如果ip中有''255'':
尝试:
打印删除,ip
ips.remove(ip)
除了异常,e:
打印e
打印ips
time.sleep(5)

有人告诉我,我要疯了;)



rbt写道:< blockquote class =post_quotes>要么我疯了又要错过这里显而易见的或者这个代码有什么错误。这个列表的元素5表示它不包含
字符串255,当它'* ALL *它包含...为什么它会认为???

import时间

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中的ip:
如果ip中有''255'':
尝试:
打印删除,ip
ips.remove(ip)
除了异常,e:
打印e
打印ips
time.sleep(5)

有人告诉我,我要疯了;)




你疯了。 ;)


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:

.... #rebread statement:

.... print" looking at",ip

。 ...如果在IP中''255'':

....尝试:

....打印删除,ip

.... ips.remove(ip)

....除了Exception,e:

....打印e

....

查看255.255.255.255

删除255.255.255.255

查看198.82.247.98

查看127.0.0.1

查看255.0.0.0

删除255.0.0.0

查看128.173.255.34

删除128.173.255.34


注意如何跳过列表中的元素。问题是

你在迭代它时修改一个列表。


为什么你不尝试列表理解:


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如果''255''不在ip中]

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


史蒂夫


rbt写道:

要不是我疯了我我错过了这里明显的或者这个代码有什么错误。这个列表的元素5表示它不包含
字符串255,当它'* ALL *它包含...为什么它会认为???

import时间

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中的ip:
如果ip中有''255'':
尝试:
打印删除,ip
ips.remove(ip)
除了异常,e:
打印e
打印ips
time.sleep(5)

有人告诉我,我要疯了;)




您正在修改列表迭代它。相反,通过使用以下方式迭代

a副本:

在ips中使用
[:]:

...


问候

Steve

-

与Python开发人员和你的clpy收藏会面3月23-25

来到PyCon DC 2005 http://www.pycon.org/

Steve Holden http://www.holdenweb.com /


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 ;)

解决方案

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 ;)



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


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/


这篇关于字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆