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

      

  • 相关阅读:
    【转】双口RAM
    Beep使用
    fcntl函数
    ioctl() 参数
    线程属性:pthread_attr_t
    GPIO
    Linux CGI编程基础
    看门狗watchdog
    Linux库知识大全
    linux进程间通讯
  • 原文地址:https://www.cnblogs.com/18128319239F/p/8653177.html
Copyright © 2011-2022 走看看