.读入待分析的字符串
分解提取单词
计数字典
排除语法型词汇
排序
输出TOP(20)
fo = open('新建文本文档.txt','r') new=fo.read() exc={'the','a','to','of','and','in','that','on',''} new=new.lower() for i in ',."-': new=new.replace(i,' ') words=new.split(' ') print(words) dic={} keys=set(words) keys=keys-exc print(keys) for i in keys: dic[i]=words.count(i) print(dic) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for i in range(20): print(wc[i]) fo.close()