Python简化代码,判断是否包含指定字符串的代码简化方法
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 re
if 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. 用正则表达式
玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd19633.html
相关文章 Recommend
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速简洁的将两个队列变成字
- • python re模块中的 match()和group()疑问
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • git能向远程仓库Pull,不能向远程仓库pu
- • Python3 来定义类时, 为何该值不是所有实例
- • IO瓶颈: python open读写文件时如何实现异步
- • python写一个递归函数
- • 在python里面如果更好的进行字符过滤?
- • 用户名跟ID都可以登录的sql语句怎么样来
- • 关于Python类中super的问题?
- • python超速遍历文件夹下面三十几万个txt文
- • celery可以动态的添加任务吗
- • python 收到的数据是b'\x81\x84t\xdeL\x16\x00\x
图文精华 RECOMMEND
-
scrapy框架里面用link extractor怎么能
-
python {}.fromkeys创建字典append添加操
-
python3 类型Type str doesn't support th
-
python里面为什么系统的时区是东八
-
Flask框架怎么来将jsonify返回的js
-
Python科学计算:一般是怎么处理
热点文章 HOT
- 学习Python有什么好的书籍推荐?
- Python匿名函数 Lambda表达式作用
- Python与Java、C、Ruby、PHP等编程语言有什么
- Python 正则中文网页字符串提取问题
- 如何为实时性应用存取经纬度?django my
- 想用python做个客户端,在二维码登录这个地
- 有让IDE可识别Python函数参数类型的方法吗
- Python字符串转换成列表正则疑问