if title.find("aaa")>=0 or title.find("bbb")>=0 or title.find("ccc")>=0:
print title
请问,以上代码如何简化呢?
if title in ["aaa","bbb","ccc"]:
print title
这样肯定是不行的,不知道可以如何简化呢?
注:我想事先创建个字符串数组,其中包含要过滤的短语
if title.find("aaa")>=0 or title.find("bbb")>=0 or title.find("ccc")>=0:
print title
请问,以上代码如何简化呢?
if title in ["aaa","bbb","ccc"]:
print title
这样肯定是不行的,不知道可以如何简化呢?
注:我想事先创建个字符串数组,其中包含要过滤的短语
def FindEx(str, modes):
result = [mode in str for mode in modes]
return True in result
你也可以把这整合成一句代码
2. 用正则表达式
我觉得每个人都有自己的使用习惯,上面的方法都可以,看自己个人喜好喽,当然至于有木有涉及到效率问题,就木有研究哈,偶是python新人。
import re
if re.search('aaa|bbb|ccc,title):
print "OK"
4 回答970 阅读
1 回答714 阅读
1 回答696 阅读
1 回答522 阅读
643 阅读
586 阅读
569 阅读