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

#1,
FFile = open('F.txt','r')
FTest = FFile.read()     
FFile.close()
print(FTest)
#2,
print(FTest.replace(',',' '))
print(FTest.replace("'",' '))
print(FTest)
#3,
print(FTest.split(' '))
FList = FTest.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[1])
#7,
FCountFile = open('FCount.txt',mode='a',encoding='utf-8')
for i in range(len(wordCountList)):
    FCountFile.write(str(wordCountList[i][1])+' '+wordCountList[i][0]+'\n')
FCountFile.close()

期末综合大作业:词频统计_第1张图片

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