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生成pdf文件的方法
Aug 04 Python
Python的动态重新封装的教程
Apr 11 Python
python访问系统环境变量的方法
Apr 29 Python
更换Django默认的模板引擎为jinja2的实现方法
May 28 Python
Python寻找两个有序数组的中位数实例详解
Dec 05 Python
解决Python3 被PHP程序调用执行返回乱码的问题
Feb 16 Python
使用python写的opencv实时监测和解析二维码和条形码
Aug 14 Python
Django 拼接两个queryset 或是两个不可以相加的对象实例
Mar 28 Python
Keras使用ImageNet上预训练的模型方式
May 23 Python
利用python绘制中国地图(含省界、河流等)
Sep 21 Python
浅谈Python数学建模之数据导入
Jun 23 Python
Python matplotlib绘制雷达图
Apr 13 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
一个odbc连mssql分页的类
2006/10/09 PHP
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
php数据库备份还原类分享
2014/03/20 PHP
PHP简单读取PDF页数的实现方法
2016/07/21 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
2017/11/10 PHP
CI框架教程之优化验证码机制详解【验证码辅助函数】
2019/04/16 PHP
idTabs基于JQuery的根据URL参数选择Tab插件
2012/04/11 Javascript
jquery中change()用法实例分析
2015/02/06 Javascript
JavaScript中如何使用cookie实现记住密码功能及cookie相关函数介绍
2016/11/10 Javascript
vue.js模仿京东省市区三级联动的选择组件实例代码
2017/11/22 Javascript
基于Angularjs-router动态改变Title值的问题
2018/08/30 Javascript
浅谈angular2子组件的事件传递(任意组件事件传递)
2018/09/30 Javascript
JavaScript键盘事件常见用法实例分析
2019/01/03 Javascript
Vue+tracking.js 实现前端人脸检测功能
2020/04/16 Javascript
JS中箭头函数与this的写法和理解
2021/01/14 Javascript
[01:03:36]Ti4 循环赛第三日DK vs Titan
2014/07/12 DOTA
[38:32]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第二局
2016/02/26 DOTA
caffe binaryproto 与 npy相互转换的实例讲解
2018/07/09 Python
python使用Paramiko模块实现远程文件拷贝
2019/04/30 Python
python多任务之协程的使用详解
2019/08/26 Python
python redis 批量设置过期key过程解析
2019/11/26 Python
Python3自动生成MySQL数据字典的markdown文本的实现
2020/05/07 Python
django 模版关闭转义方式
2020/05/14 Python
Mio Skincare美国官网:身体紧致及孕期身体护理
2017/03/05 全球购物
瑞典快乐袜子:Happy Socks
2018/02/16 全球购物
全球性的在线鞋类品牌:Public Desire
2019/04/03 全球购物
英国地毯卖家:The Rug Seller
2019/07/18 全球购物
美国亚马逊旗下时尚女装网店:SHOPBOP(支持中文)
2020/10/17 全球购物
建筑工程技术应届生求职信
2013/11/17 职场文书
教育实习生的自我评价分享
2013/11/21 职场文书
护士求职推荐信范文
2013/11/23 职场文书
园林毕业生自我鉴定范文
2013/12/29 职场文书
幼儿园运动会入场词
2014/02/10 职场文书
幼儿园六一儿童节主持节目串词
2014/03/21 职场文书
歌咏比赛主持词
2015/06/29 职场文书
党校培训学习心得体会
2016/01/06 职场文书