#1, letFile = open(‘let.txt‘,‘r‘) letTest = letFile.read() letFile.close() print(letTest) #2, print(letTest.replace(‘,‘,‘ ‘)) print(letTest.replace("‘",‘ ‘)) print(letTest) #3, print(letTest.split(‘ ‘)) letList = letTest.split(‘ ‘) #4, letSet = set(letList) print(letSet) letDict = {} for word in letSet: letDict[word] =letList.count(word) print(letDict) for d in letDict: print(d,letDict[d]) #5, wordCountList = list(letDict.items()) print(wordCountList) wordCountList.sort(key=lambda x:x[1],reverse=True) print(wordCountList) #6, for i in range(20): print(wordCountList[1]) #7, bigCountFile = open(‘letCount.txt‘,mode=‘a‘,encoding=‘utf-8‘) for i in range(len(wordCountList)): letCountFile.write(str(wordCountList[i][1])+‘ ‘+wordCountList[i][0]+‘\n‘) letCountFile.close()
原文:https://www.cnblogs.com/-wingying/p/9206208.html