词频统计

file = open('song.txt', 'r')  # 只读打开文件
lyrics = file.read()  # 读取文件内容
file.close()  # 关闭文件资源
sep=''',.?!:'-'''
for c in sep:
    song = song.replace(c,'')
wordList = song.lower().split()

wordDict={}

for w in wordList:
    wordDict[w]=wordDict.get(w,0)+1
    dictList = list(wordDict.items())
    dictList.sort(key=lambda x:x[1],reverse=True)

    for w in dictList:
        print(w)

  

 

posted @ 2018-03-26 16:47  113杨链新  阅读(109)  评论(0编辑  收藏  举报