首页 > 其他 > 详细

4染发放g

时间:2018-06-20 21:58:14      阅读:197      评论:0      收藏:0      [点我收藏+]
#1.
theFile = open(‘the.txt‘,mode=‘r‘,encoding=‘utf-8‘)
theText = theFile.read()  # 从文件里读出全部文本,字符串
theFile.close()
print(theText)

#2.
replaceList = [‘,‘,‘.‘,"‘",‘\n‘]
for c in replaceList:
    theText = theText.replace(c,‘ ‘)  # 替换掉所以标点符号
theText = theText.replace(‘  ‘,‘ ‘)
print(theText)

#3.
print(theText.split(‘ ‘))
theList = theText.split(‘ ‘)  #列表 出现的单词序列

#4.
theSet = set(theList)  # 集合 有哪些单词
print(theSet)

theDict = {}  # 字典:每个单词的词频统计次数
for word in theSet:
    theDict[word] = theList.count(word)
print(theDict)


#5.排序
wordCountList = list(theDict.items())  #字典没有顺序,不能排序,转换成列表进行排序
print(wordCountList)
wordCountList.sort(key=lambda x:x[1],reverse=True) # 进行排序
print(wordCountList)

‘‘‘
#6.输出top20
for i in range(20):
    print(wordCountList[i])

  

4染发放g

原文:https://www.cnblogs.com/biubiuojbk/p/9206234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!