zoukankan      html  css  js  c++  java
  • 综合练习:词频统计

    # fo = open('zzzz.txt;','r')
    # s = fo.read()
    # fo.close()
    # print(s)
    
    s = '''Though people have been always educated to save water, we can see they waste water all the time. For example, sometimes I can see children wash their hands for a long time and when they go away, they always forget to close the faucet. The most common situation I see is in the public toilet that people just walk away after using faucet and they don’t meant to close it. They are wasting water and they should be condemned. Water conservation is in need because in most part of the world, people don’t have enough water and we need water to keep alive. On March 22nd , which is a day to call for the public to save water, if not, we will face the emergent situation. So it is everybody’s duty not to waste water.'''
    sep = '''":;,'.!?'''
    exclude = {'the','and','of','to','in','a','he','she','for','if','on','so','in','on'}
    for i in sep:
        s=s.replace(i,"")
    
    wordList = s.lower().split()
    wordDict = {}
    wordSet = set(wordList)
    
    wordSet = set(wordList) - exclude
    
    for w in wordSet:
        wordDict[w] = wordList.count(w)
    
    # for w in wordSet:
    #     wordDict[w] = wordDict.get(w,0)+1
    
    dictList = list(wordDict.items())
    dictList.sort(key= lambda x:x[1],reverse=True)
    
    # for w in wordDict:
    #         print(w,wordDict[w])
    for i in range(20):
        print(dictList[i])
    

      

  • 相关阅读:
    Delphi实现文件关联
    用Delphi实现文件关联
    Delphi 7使用自定义图标关联文件类型
    redux-form的学习笔记
    妙用 `package.json` 快速 `import` 文件(夹)
    【webpack2】-- 入门与解析
    黑科技:CSS定制多行省略
    DOM操作和样式操作库的封装
    webstorm 2016 激活破解
    js/jq仿window文件夹框选操作插件
  • 原文地址:https://www.cnblogs.com/18128319239F/p/8653177.html
Copyright © 2011-2022 走看看