python实现统计word的小程序,pythonword小程序,Python语言: py


Python语言: python实现统计word的小程序# -*- coding:utf-8 -*- import redef mycmp (x,y):    if x[1] > y[1]:        return 1    elif x[1] < y[1]:        return -1    else:        if x[0] > y[0]:            return -1        elif x[0] ==y[0]:            return 0        else:            return 1file = open("word.py")text = file.read()wordList = str.split(re.sub(r'\\W|\\d',' ',text))wordList= zip(set(wordList),map(lambda x:wordList.count(x),set(wordList)))wordList.sort(reverse=True,cmp=mycmp)for word in wordList:    print word[0],":",word[1]#该片段来自于http://byrx.net

评论关闭