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])
    

      

  • 相关阅读:
    工作中遇到新知识应该怎么办
    Java中的集合
    JSTL学习(二)自定义标签库
    别跟我扯依赖注入
    经典算法的分析
    Debian
    C 底层细节【转】
    C文件操作 【转】
    利用strstr和sscanf解析GPS信息
    算法学习建议 ACM()转
  • 原文地址:https://www.cnblogs.com/18128319239F/p/8653177.html
Copyright © 2011-2022 走看看