python字母计数-使用Python对文档单词进行计数

  • Post author:
  • Post category:python


做hacker.org上面的题目时,遇到了一个题目需要对RFC3280种长度为9的单词进行计数,并找出这些单词中出现次数最多的那个:Didactic Byte

RFC3280文档有7000多行,靠人工是数不出来的,解决这种是就得编程了,而且很明显,在处理此类问题时脚本式比较方便的,果断选择python

先是将题意理解错了,理解为出现次数最多的九个字母, 然后等到程序运行好提交答案时才发现不对,唉,真是汗颜

既然程序写出来了,还是将其贴出来吧

文档中字母频率计数:

# find the occurence of single letter from a document

#import fileinput

test = “”

dicts = {}

# read content from a document

# we could use readline,readlines etc.

# also we could use fileinput.input(filename)

text = open(“RFC3280.txt”)

for line in text.readlines():

test += line.lower().replace(” “,””)

# use dictionary to store the occurence and it”s letter

for i in xrange(97,123):

letter = chr(i)

count = test.count(chr(i))

#print “the accoun