Python实现对文件进行单词划分并去重排序操作示例


Posted in Python onJuly 10, 2018

本文实例讲述了Python实现对文件进行单词划分并去重排序操作。分享给大家供大家参考,具体如下:

文件名:test1.txt

文件内容:

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

样例输出:

Enter file name: "test1.txt"
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']

PR4E 用 append的写法:(二重循环)

import sys
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
  line = line.rstrip()
  words = line.split()
  for word in words:
    if word not in lst:
      lst.append(word)
lst.sort()
print(lst)

自己一重循环写法:

import string
fname = input("Enter file name: ")
fh = open(fname)
l = list()
for line in fh:
  line = line.rstrip()
  l = l + list(line.split())
s = list(set(l))
s.sort()
print(s)
Python 相关文章推荐
python使用新浪微博api上传图片到微博示例
Jan 10 Python
Python实现模拟时钟代码推荐
Nov 08 Python
python字符串str和字节数组相互转化方法
Mar 18 Python
python的dataframe和matrix的互换方法
Apr 11 Python
python实现决策树分类(2)
Aug 30 Python
django中使用POST方法获取POST数据
Aug 20 Python
keras获得model中某一层的某一个Tensor的输出维度教程
Jan 24 Python
python随机模块random的22种函数(小结)
May 15 Python
OpenCV4.1.0+VS2017环境配置的方法步骤
Jul 09 Python
Python结合Window计划任务监测邮件的示例代码
Aug 05 Python
Python 3.9的到来到底是意味着什么
Oct 14 Python
Python Matplotlib绘制条形图的全过程
Oct 24 Python
python3中函数参数的四种简单用法
Jul 09 #Python
python3学习之Splash的安装与实例教程
Jul 09 #Python
Python基于sklearn库的分类算法简单应用示例
Jul 09 #Python
Python不使用int()函数把字符串转换为数字的方法
Jul 09 #Python
python中ASCII码和字符的转换方法
Jul 09 #Python
python中ASCII码字符与int之间的转换方法
Jul 09 #Python
Python 十六进制整数与ASCii编码字符串相互转换方法
Jul 09 #Python
You might like
php支持中文字符串分割的函数
2015/05/28 PHP
Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
2013/10/11 Javascript
jquery教程ajax请求json数据示例
2014/01/13 Javascript
js控制再次点击按钮之间的间隔时间可防止重复提交
2014/08/01 Javascript
使用js实现的简单拖拽效果
2015/03/18 Javascript
jQuery实现图片左右滚动特效
2020/04/20 Javascript
js检测用户输入密码强度
2015/10/22 Javascript
jQuery的文档处理程序详解
2016/05/10 Javascript
对js中回调函数的一些看法
2016/08/29 Javascript
Promise.all中对于reject的处理方法
2018/08/01 Javascript
NodeJs实现简易WEB上传下载服务器
2019/08/10 NodeJs
解决layui使用layui-icon出现默认图标的问题
2019/09/11 Javascript
[01:10:57]Liquid vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
2014/04/25 Python
python网络编程学习笔记(九):数据库客户端 DB-API
2014/06/09 Python
python新手经常遇到的17个错误分析
2014/07/30 Python
Python中使用tarfile压缩、解压tar归档文件示例
2015/04/05 Python
详解Python开发中如何使用Hook技巧
2017/11/01 Python
微信跳一跳游戏python脚本
2020/04/01 Python
Python爬虫:将headers请求头字符串转为字典的方法
2019/08/21 Python
Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的
2020/04/20 Python
css3media响应式布局实例
2016/07/08 HTML / CSS
西班牙语在线票务市场:SuperBoletería
2019/06/10 全球购物
C#基础面试题
2016/10/17 面试题
Java里面Pass by value和Pass by Reference是什么意思
2016/05/02 面试题
学生实习推荐信范文
2013/11/26 职场文书
教师党员思想汇报
2014/01/06 职场文书
孝老爱亲模范事迹
2014/01/24 职场文书
优秀小学生家长评语
2014/01/30 职场文书
家长给学校的建议书
2014/05/15 职场文书
买房子个人收入证明
2014/10/12 职场文书
见习报告怎么写
2014/10/31 职场文书
音乐课外活动总结
2015/05/09 职场文书
2015年新农村建设工作总结
2015/05/22 职场文书
2016党员学习作风建设心得体会
2016/01/21 职场文书
2016年国庆节假期旅游工作总结
2016/04/01 职场文书