标签:ext for range http alt reverse word print replace
#1. loveFile = open(‘love.txt‘,mode=‘r‘,encoding=‘utf-8‘) loveText = loveFile.read() loveFile.close() print(loveText) #2. replaceList = [‘,‘,‘.‘,"‘",‘\n‘] for c in replaceList: loveTxt = loveText.replace(c,‘ ‘) print(loveText) #3. print(loveText.split(‘ ‘)) loveList = loveText.split(‘ ‘) #4. loveSet = set(loveList) print(loveSet) loveDict = set(loveList) print(loveSet) loveDict = {} for word in loveSet: loveDict[word] =loveList.count(word) print(loveDict) for d in loveDict: print(d,loveDict[d]) wordCountList = list(loveDict.items()) print(wordCountList) wordCountList.sort(key=lambda x:x[1],reverse=True) print(wordCountList) for i in range(20): print(wordCountList[i]) loveCountFile = open(‘loveCount.txt‘, mode=‘a‘,encoding=‘utf-8‘) for i in range(len(wordCountList)): loveCountFile.write(str(wordCountList[i][1])+‘ ‘+wordCountList[i][0]+‘\n‘) loveCountFile.close()
标签:ext for range http alt reverse word print replace
原文地址:https://www.cnblogs.com/Minwon/p/9206137.html