【发布时间】:2013-11-19 21:31:06
【问题描述】:
我试图在这个不同的程序中按字母顺序查找文本文件中最常用的单词。
例如,单词:“that”是文本文件中出现频率最高的单词。所以,应该先打印出来:“那个#”
它需要采用这种格式作为程序和下面的答案:
d = dict()
def counter_one():
d = dict()
word_file = open('gg.txt')
for line in word_file:
word = line.strip().lower()
d = counter_two(word, d)
return d
def counter_two(word, d):
d = dict()
word_file = open('gg.txt')
for line in word_file:
if word not in d:
d[word] = 1
else:
d[word] + 1
return d
def diction(d):
for key, val in d.iteritems():
print key, val
counter_one()
diction(d)
它应该在 shell 中运行类似这样的东西:
>>>
Words in text: ###
Frequent Words: ###
that 11
the 11
we 10
which 10
>>>
【问题讨论】:
标签: python word frequency numerical alphabetical