期末综合大作业:词频统计

#1.
letFile = open('let.txt',mode="r",encoding='utf-8')
letText = letFile.read()
letFile.close()
print(letText)

#2.
replaceList = [',','.',"'",'\n']
for c in replaceList:
    letText = letText.replace(c,' ')
print(letText)

#3.
print(letText.split(' '))
letList = letText.split(' ')

#4.
letSet = set(letList)
print(letSet)

letDict = {}
for word in letSet:
    letDict[word] = letList.count(word)

print(letDict)
for d in letDict:
    print(d,letDict[d])

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

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

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

 

你可能感兴趣的:(期末综合大作业:词频统计)