标签:字典 replace 排除 lower col div 字符串 reverse style
1.读入待分析的字符串
2.分解提取单词
3.计数字典
4.排除语法型词汇
5.排序
6.输出TOP(20)
fo=open(‘test.txt‘,‘r‘) a=fo.read() fo.close() exc={‘the‘,‘a‘,‘to‘,‘of‘,‘and‘,‘in‘,‘that‘,‘on‘,‘‘} a=a.lower() for i in ‘.,‘: a=a.replace(i,‘ ‘) words=a.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) print(wc) for i in range(20): print(wc[i])
标签:字典 replace 排除 lower col div 字符串 reverse style
原文地址:http://www.cnblogs.com/0020l/p/7598575.html