标签:code 文件 dict print ict int open write item
#1 gFile = open(‘g.txt‘,mode=‘r‘,encoding=‘utf-8‘) gText = gFile.read() gFile.close() print(gText) #2 replaceList = [‘,‘,‘.‘,"‘",‘\n‘] for c in replaceList: gText=gText.replace(c,‘ ‘) gText = gText.replace(‘ ‘,‘ ‘) print(gText) #3 print(gText.split(‘ ‘)) gfList=gText.split(‘ ‘) #4 gSet=set(gList) print(gSet) gDict = {} for word in gSet: gDict[word] = gList.count(word) print(gDict) # 5.排序 wordCountList = (gDict.items()) print(wordCountList) wordCountList.sort(key=lambda x:x[1],reverse=True) print(wordCountList) #6.输出Top20 for i in range(20): print(wordCountList[i]) ‘‘‘ #7.写入文件 gCountFile = open(‘gCount.txt‘, mode=‘a‘,encoding=‘utf-8‘) for i in range(len(wordCountList)): gCountFile.write(str(wordCountList[i][1]+‘ ‘+wordCountList[i][0]+‘\n‘) gCountFile.close() ‘‘‘
标签:code 文件 dict print ict int open write item
原文地址:https://www.cnblogs.com/zhangkef97/p/9206251.html