词频统计

f = open('news.txt','r')
news = f.read()
f.close()
print(news)

sep = '''.,?'!:"'''
exclude = {'the','and','of','to','and','he','for'}
# 将sep里面的字符替换为空格
for c in sep:
    news = news.replace(c," ")

wordList = news.lower().split()
print(wordList)

wordDict = {}
wordSet = set(wordList)
for w in wordSet:
    wordDict[w] = wordList.count(w)
    # print(wordDict[w],wordList.count(w))
dictList = list(wordDict.items())
dictList.sort(key=lambda  x:x[1],reverse=True)
a = dictList
# for w in dictList:
#     print(w)
for i in range(10):
    print(dictList[i])
exit()
for w in wordList:
    wordDict[w] = wordDict.get(w,0)+1
for w in exclude:
    wordDict.pop(w)
dictList = list(wordDict.items())
print(dictList)


  

原文地址:https://www.cnblogs.com/a13798508446/p/8649761.html