您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python统计文本字符串里单词出现频率的方法

2024/3/20 12:18:46发布22次查看
本文实例讲述了python统计文本字符串里单词出现频率的方法。分享给大家供大家参考。具体实现方法如下:
# word frequency in a text# tested with python24 vegaseat 25aug2005# chinese wisdom ...str1 = man who run in front of car, get tired.man who run behind car, get exhausted.print original string:print str1print# create a list of words separated at whitespaceswordlist1 = str1.split(none)# strip any punctuation marks and build modified word list# start with an empty listwordlist2 = []for word1 in wordlist1: # last character of each word lastchar = word1[-1:] # use a list of punctuation marks if lastchar in [,, ., !, ?, ;]: word2 = word1.rstrip(lastchar) else: word2 = word1 # build a wordlist of lower case modified words wordlist2.append(word2.lower())print word list created from modified string:print wordlist2print# create a wordfrequency dictionary# start with an empty dictionaryfreqd2 = {}for word2 in wordlist2: freqd2[word2] = freqd2.get(word2, 0) + 1# create a list of keys and sort the list# all words are lower case alreadykeylist = freqd2.keys()keylist.sort()print frequency of each word in the word list (sorted):for key2 in keylist: print %-10s %d % (key2, freqd2[key2])
希望本文所述对大家的python程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product