用python编写一个统计分析某text文件里的字、空格、其他字符的数量并统计排序频率最高的前10个字

上传人:c****e 文档编号:144916845 上传时间:2020-09-14 格式:PDF 页数:2 大小:40.12KB
返回 下载 相关 举报
用python编写一个统计分析某text文件里的字、空格、其他字符的数量并统计排序频率最高的前10个字_第1页
第1页 / 共2页
用python编写一个统计分析某text文件里的字、空格、其他字符的数量并统计排序频率最高的前10个字_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《用python编写一个统计分析某text文件里的字、空格、其他字符的数量并统计排序频率最高的前10个字》由会员分享,可在线阅读,更多相关《用python编写一个统计分析某text文件里的字、空格、其他字符的数量并统计排序频率最高的前10个字(2页珍藏版)》请在金锄头文库上搜索。

1、# 本游戏旨在计算并打印出一个文件中内容的统计数据。比如,以只读方式统计并打印 出文件包含多少个字符、行和单词, # 并统计出现次数最多的前 10 个单词,按出现次数排列好。 (文件类型主要是 .txt .xls .doc- written by LiSongbo Words = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,w, x, y, z, , -, def normalize(Rocky): #- written by LiSongbo result = for n in Rocky.lower():

2、 if n in Words: result += n return result def make_frequent_dict(Lee): #- written by LiSongbo Lee = normalize(Lee) words = Lee.split() R = # return a dictionary whose keys are the words of Lee,and whose values are the couts of those words for w in words: if w in R: Rw += 1 else: Rw=1 # if this tis t

3、he first time for w comes up in dictionary R,set its count as 1 return R def file_count(fname): #- written by LiSongbo print statistics for the given file. Rocky_Lee = open(fname,r).read() num_chars = len(Rocky_Lee) num_lines = Rocky_Lee.count(n) d = make_frequent_dict(Rocky_Lee) num_words = sum(dw

4、for w in d) # 计算 Rocky_Lee 包含多少个单词 lst = (dw,w) for w in d lst.sort() lst.reverse() print(This file %s has: % fname) print( %s characters % num_chars) print( %s lines% num_lines) print( %s words% num_words) print(nThe top 10 most frequent words are: ) i = 1 for count,word in lst0:10: print(%2s. %4s

5、%s % (i,count,word) i += 1 def main(): #- written by LiSongbo file_count(test1.txt) if _name_ = _main_: main() # 运行结果 This file test1.txt has: 84709 characters 276 lines 14547 words The top 10 most frequent words are: 1. 1124 the 2. 648 of 3. 474 and 4. 333 a 5. 328 to 6. 318 in 7. 224 i 8. 161 was 9. 140 which 10. 126 it Process finished with exit code 0 版权声明:本文为 CSDN 博主顶洞达人的原创文章,遵循 CC 4.0 BY-SA 版权协议,转 载请附上原文出处链接及本声明。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > Python

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号