【发布时间】:2013-11-08 19:21:16
【问题描述】:
我必须做的练习有点问题: 基本上任务是打开一个url,把它转换成给定的格式,然后统计给定字符串在文本中出现的次数。
import urllib2 as ul
def word_counting(url, code, words):
page = ul.urlopen(url)
text = page.read()
decoded = ext.decode(code)
result = {}
for word in words:
count = decoded.count(word)
counted = str(word) + ":" + " " + str(count)
result.append(counted)
return finale
我应该得到的结果类似于“ word1: x, word2: y, word3: z ”,其中 x,y,z 是出现次数。但似乎我只得到一个数字,当我尝试运行测试程序时,我得到的结果只有第一次出现 9,第二次出现 14,第三次出现 5,缺少其他出现和整个计数值. 我究竟做错了什么?提前致谢
【问题讨论】:
-
你可能想看看 Counter dict:docs.python.org/2/library/collections.html#collections.Counter
标签: python