python多行代码简化
python中,可以把多行代码简化为一行,把for循环和if条件判断都集中到一行里来写,示例如下:
>>> from nltk.corpus import stopwords
>>> english_stopwords = stopwords.words('english')#加载nltk中的英文停用词数据
#创建一个列表,内含3个单词列表
>>> texts_tokenized = [['writing', 'ii', 'rhetorical', 'composing', 'rhetorical', 'composing'],['engages', 'series', 'interactive', 'reading'],['research', 'composing', 'activities', 'along', 'assignments', 'designed', 'help']]
#用多行代码对texts_tokenized去停用词
>>> text_filtered_stopwords = [[word for word in document if not word in english_stopwords] for document in texts_tokenized]
>>> text_filtered_stopwords
[['writing', 'ii', 'rhetorical', 'composing', 'rhetorical', 'composing'], ['engages', 'series', 'interactive', 'reading'], ['research', 'composing', 'activities', 'along', 'assignments', 'designed', 'help']]
然后改成用多行的常规写法:
>>> texts_tokenized = [['writing', 'ii', 'rhetorical', 'composing', 'rhetorical', 'composing'],['engages', 'series', 'interactive', 'reading'],['research', 'composing', 'activities', 'along', 'assignments', 'designed', 'help']]
>>> documents = []
>>> texts_filtered_stopwords =[]
>>> for document in texts_tokenized:
for word in document:
if word not in english_stopwords:
documents.append(word)
texts_filtered_stopwords.append(document) >>> texts_filtered_stopwords
[['writing', 'ii', 'rhetorical', 'composing', 'rhetorical', 'composing'], ['engages', 'series', 'interactive', 'reading'], ['research', 'composing', 'activities', 'along', 'assignments', 'designed', 'help']]
可以看到得出一样的结果,但是代码的效率和简洁程度大大提升
python多行代码简化的更多相关文章
- Python第一行代码
Python版本:Python 3.6.1 0x01 命令行交互 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计 ...
- Python 1行代码实现文本分类(实战笔记),含代码详细说明及运行结果
Python 1行代码实现文本分类(实战笔记),含代码详细说明及运行结果 一.详细说明及代码 tc.py =============================================== ...
- Python 5 行代码的神奇操作
Python 语言实现功能直接了当,简明扼要,今天咱们就来一起看看 Python 5 行代码的神奇操作! 1.古典兔子问题 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语 ...
- 利用Python几行代码批量生成验证码
几行代码批量生成authCode 整体步骤: 1.创建图片 2.创建画笔 3.在图片上生成点 4.在图片上画线 5.在图片在画圆 6.在图片在写文本 7.在图片在生成指定字体的文本 代码奉上 #!/u ...
- Python 3 行代码 5 秒抠图的 AI 神器,根本无需 PS
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 苏克1900 PS:如有需要Python学习资料的小伙伴可以加点击下 ...
- python——5行代码采集3000+上市公司信息
毕业季也到了找工作的季节了,很多小伙伴都会一家一家的公司去看,这得多浪费时间啊.今天用Python教大家怎么采集公司的信息,相信大家会很喜欢这个教程的,nice! 基本环境配置 版本:Python3 ...
- Python几行代码实现邮件发送
话不多说直接进入正题 首先我们需要安装一个名为'zmail'的包,终端执行'pip install zmail'即可实现安装. 直接上代码 import zmail mail = { 'subject ...
- 10分钟教你用python 30行代码搞定简单手写识别!
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...
- 比PS还好用!Python 20行代码批量抠图
你是否曾经想将某张照片中的人物抠出来,然后拼接到其他图片上去,从而可以即使你在天涯海角,我也可以到此一游? 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在 ...
随机推荐
- GraphQL: Object doesn't support property or method 'from'
From: https://github.com/graphql/graphiql/issues/688 psyCodelist commented 11 days ago Hi, Thank you ...
- hustoj升级
sudo su svn up hustoj-read-only cd hustoj-read-only cd core sudo ./make.sh 有冲突 全部回答r http://www.hu ...
- centos7 mysql数据库安装和配置(转, 未验证)
一.系统环境 yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 ...
- jquery ajax IE
在ie上会出现,get/post 只调用一次的现象 解决方法: cache:false http://stackoverflow.com/questions/8841425/how-to-set-ca ...
- 【SqlServer】SqlServer的异常处理
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...
- 《Redis入门指南(第2版)》读后感
今天刚刚将此书看完,现在还能记住一些内容,还有一些感慨感想,正好又想写点什么了就随便记录一下吧!也许灵感明天就消失了呢? 首先觉得作者非常的厉害,年纪轻轻的就写出了这么一本非常不错的书籍! 然后就是对 ...
- OpenCV 学习笔记 04 深度估计与分割
本章节主要是使用深度摄像头的数据来识别前景区和背景区,这样就可以分别对前景和背景做不同的处理. 1 创建模块
- 每日英语:Dashing the China Dream
Much has been said about what the 'China Dream' really means to many Chinese -- whether it is nation ...
- 2. RNN神经网络模型的不同结构
1. RNN神经网络模型原理 2. RNN神经网络模型的不同结构 3. RNN神经网络-LSTM模型结构 1. 前言 RNN( Recurrent Neural Network 循环(递归)神经网络) ...
- 大津法---OTSU算法
简介: 大津法(OTSU)是一种确定图像二值化分割阈值的算法,由日本学者大津于1979年提出.从大津法的原理上来讲,该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景 ...