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

rwgwgrger

时间:2018-06-18 18:25:09      阅读:129      评论:0      收藏:0      [点我收藏+]

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

  技术分享图片

rwgwgrger

标签:str   pytho   bubuko   rev   pre   read   items   list   ISE   

原文地址:https://www.cnblogs.com/A1-1/p/9195484.html

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