【发布时间】:2011-09-21 10:17:18
【问题描述】:
尝试清理 python 列表,我能够删除完全匹配的字符串。如何删除部分匹配项?
exclude = ['\n','Hits','Sites','blah','blah2','partial string','maybe here']
newlist = []
for item in array:
if item not in exclude:
newlist.append(item)
这里的问题是“item not in exclude”... 完全匹配。
我应该使用以下方法吗:
s = "This be a string"
if s.find("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."
在某种程度上我回答了我自己的问题 :) 我猜是否有替代 'in' 的操作数?
谢谢
【问题讨论】:
-
不是很清楚,你想要什么。另外,您在上面的代码中定义的
array是什么? -
这显然取决于“部分匹配”的定义:什么被认为是匹配,什么不是?
-
string.find(s, sub[, start[, end]])