南昌哪里做网站,最快的新闻发布平台,重庆网站建设之,在线电子印章制作本文实例讲述了Python实现统计英文文章词频的方法。分享给大家供大家参考#xff0c;具体如下#xff1a;应用介绍#xff1a;统计英文文章词频是很常见的需求#xff0c;本文利用python实现。思路分析#xff1a;1、把英文文章的每个单词放到列表里#xff0c;并统计列表…本文实例讲述了Python实现统计英文文章词频的方法。分享给大家供大家参考具体如下应用介绍统计英文文章词频是很常见的需求本文利用python实现。思路分析1、把英文文章的每个单词放到列表里并统计列表长度2、遍历列表对每个单词出现的次数进行统计并将结果存储在字典中3、利用步骤1中获得的列表长度求出每个单词出现的频率并将结果存储在频率字典中4、以字典键值对的“值”为标准对字典进行排序输出结果(也可利用切片输出频率最大或最小的特定几个因为经过排序sorted()函数处理后单词及其频率信息已经存储在元组中所有元组再组成列表。)代码实现fin open(The_Magic_Skin _Honore_de_Balzac.txt) #the txt is up#to youlinesfin.readlines()fin.close()transform the article into word listdef words_list():chardigitABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 all_lines for line in lines:one_linefor ch in line:if ch in chardigit:one_line one_line chall_lines all_lines one_linereturn all_lines.split()calculate the total number of article lists is the article listdef total_num(s):return len(s)calculate the occurrence times of every wordt is the article listdef word_dic(t):fre_dic dict()for i in range(len(t)):fre_dic[t[i]] fre_dic.get(t[i],0) 1return fre_diccalculate the occurrence times of every wordw is dictionary of the occurrence times of every worddef word_fre(w):for key in w:w[key] w[key] / totalreturn wsort the dictionaryv is the frequency of wordsdef word_sort(v):sort_dic sorted(v.items(), key lambda e:e[1])return sort_dicThis is entrance of functionsoutput is the ten words with the largest frequencytotal total_num(words_list())print(word_sort(word_fre(word_dic(words_list())))[-10:])PS这里再为大家推荐2款相关统计工具供大家参考希望本文所述对大家Python程序设计有所帮助。