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)