标签:str pytho bubuko rev pre read items list ISE
#1. geciFile = open(‘geci.txt‘,mode="r",encoding=‘utf-8‘) geciText = geciFile.read() geciFile.close() print(geciText) #2. replaceList = [‘,‘,‘.‘,"‘",‘\n‘] for c in replaceList: geciText = geciText.replace(c,‘ ‘) print(geciText) #3. print(geciText.split(‘ ‘)) geciList = geciText.split(‘ ‘) #4. geciSet = set(geciList) print(geciSet) geciDict = {} for word in geciSet: geciDict[word] = geciList.count(word) print(geciDict) for d in geciDict: print(d,geciDict[d]) #5. wordCountList = list(geciDict.items()) print(wordCountList) wordCountList.sort(key=lambda x:x[1],reverse=True) print(wordCountList) #6. for i in range(20): print(wordCountList) #7. geciCountFile = open(‘geciCount.txt‘,mode=‘a‘,encoding=‘utf-8‘) for i in range(len(wordCountList)): geciCountFile.write(str(wordCountList[i][1])+‘ ‘+wordCountList[i][0]+‘\n‘) geciCountFile.close()
标签:str pytho bubuko rev pre read items list ISE
原文地址:https://www.cnblogs.com/A1-1/p/9195484.html