国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現對文件進行單詞劃分并去重排序操作示例

2020-02-15 22:19:08
字體:
來源:轉載
供稿:網友

本文實例講述了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 sysfname = 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 stringfname = 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)

PS:這里再為大家提供幾款相關工具供大家參考使用:

在線去除重復項工具:
http://tools.jb51.net/code/quchong

在線文本去重復工具:
http://tools.jb51.net/aideddesign/txt_quchong

在線動畫演示插入/選擇/冒泡/歸并/希爾/快速排序算法過程工具:
http://tools.jb51.net/aideddesign/paixu_ys

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锡林浩特市| 大理市| 桦甸市| 湄潭县| 禹州市| 崇左市| 通海县| 拜泉县| 宜州市| 房山区| 武宁县| 常德市| 舟曲县| 奎屯市| 苗栗市| 青海省| 阿鲁科尔沁旗| 师宗县| 萍乡市| 古浪县| 潮州市| 西青区| 武宁县| 紫金县| 新营市| 清徐县| 龙门县| 锦屏县| 拜城县| 开鲁县| 乐清市| 民和| 吉木萨尔县| 苏尼特左旗| 岱山县| 定日县| 富阳市| 当涂县| 盖州市| 盖州市| 南涧|