码迷,mamicode.com
首页 > 其他好文 > 详细

123

时间:2018-06-20 22:45:03      阅读:262      评论:0      收藏:0      [点我收藏+]

标签: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()
‘‘‘

 

123

标签:code   文件   dict   print   ict   int   open   write   item   

原文地址:https://www.cnblogs.com/zhangkef97/p/9206251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!