기 말 종합 숙제: 주파수 통계

#1
f = open('Les Miserables    .txt',mode='r',encoding='utf-8')
fText = f.read()#
print(fText)

#2
replacelist = ['?','.',',',':','"',"'"]
for c in replacelist:
    fText = fText.replace(c,'')#         
print(fText)

#3
print(fText.split(' '))
fList = fText.split(' ')#         

#4
fSet = set(fList)#
print(fSet)

fDict = {}
for word in fSet:
    fDict[word]=fList.count(word)
print(fDict)
for d in fDict:
    print(d,fDict[d])

#5
wordCountList = list(fDict.items())
print(wordCountList)
wordCountList.sort(key=lambda x:x[1],reverse=True)
print(wordCountList)

#6
for i in range(20):
    print(wordCountList[i])

#7
fCountFile = open('fText.txt',mode='a',encoding='utf-8')
for i in range(len(wordCountList)):
    fCountFile.write(str(wordCountList[i][1])+' '+wordCountList[i][0]+'
') fCountFile.close()

좋은 웹페이지 즐겨찾기