Python简化代码,判断是否包含指定字符串的代码简化方法,python简化,if title.fin
Python简化代码,判断是否包含指定字符串的代码简化方法,python简化,if title.fin
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 any(str_ in title for str_ in ("aaa", "bbb", "ccc")): print title
反了吧
if "aaa" in title or "bbb" in title or "ccc" in title : print title
我觉得每个人都有自己的使用习惯,上面的方法都可以,看自己个人喜好喽,当然至于有木有涉及到效率问题,就木有研究哈,偶是python新人。
import reif re.search('aaa|bbb|ccc,title): print "OK"
if filter(lambda x: x in title, ['aaa','bbb','ccc']):
print title
用正则?其实吧别简化了,就这样也不错。
想麻烦了吧....
if [s for s in ['aaa','bbb','ccc'] if s in title]: print title
def FindEx(str, modes): result = [mode in str for mode in modes] return True in result
你也可以把这整合成一句代码
2. 用正则表达式
编橙之家文章,
相关内容
- Python与android应用实现实时消息推送的方法是?,pythona
- python utf-8编码被识别为gb2312求解决方法,pythonutf-8,# c
- Python备份离线静态站点方法是什么?,python离线,有时候
- 写一个php框架怎么做到?,php框架做到,删了吧 我只是
- 请问Beautifulsoup抓取select标签中option文本方法是什么,
- Python运维方向基本工作内容是什么,python运维工作内容
- Python爬虫完成js页面内容抓取方案是怎么样的,pythonj
- Python字符串之间如何比较,Python字符串比较,远程版本号
- python访问嵌套内数据实现如何定义,python访问嵌套数据
- python ascii格式怎么生成utf-8文件,pythonutf-8,import codec
评论关闭