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