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

 
   
#1.
aaaFile = open('aaa.txt',mode="r",encoding='utf-8')
aaaText = aaaFile.read()
aaaFile.close()
print(aaaText)

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

#3.
print(aaaText.split(' '))
aaaList = aaaText.split(' ')

#4.
aaaSet = set(aaaList)
print(aaaSet)

aaaDict = {}
for word in adcSet:
    aaaDict[word] = aaaList.count(word)

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

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

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

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

  

 
 
  

 

 

 

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