字符串到字典字数

时间:2015-05-24 04:12:31

标签: python python-3.x dictionary word-frequency

所以我在做家庭作业问题时遇到了麻烦。

  

编写一个函数word_counter(input_str),它接受一个字符串input_str,并返回一个字典映射input_str中的单词到它们的出现次数。

所以我到目前为止的代码是:

def word_counter(input_str):

'''function that counts occurrences of words in a string'''

    sentence = input_str.lower().split()

    counts = {}

    for w in sentence:
        counts[w] = counts.get(w, 0) + 1

    items = counts.items()
    sorted_items = sorted(items)

    return sorted_items

现在,当我在Python shell中运行带有word_counter("This is a sentence")等测试用例的代码时,得到以下结果:

[('a', 1), ('is', 1), ('sentence', 1), ('this', 2)]

这是必需的。但是,用于检查答案的测试代码是:

word_count_dict = word_counter("This is a sentence")
items = word_count_dict.items()
sorted_items = sorted(items)
print(sorted_items)

当我用该代码运行它时,我收到错误:

Traceback (most recent call last):
File "<string>", line 2, in <fragment>
builtins.AttributeError: 'list' object has no attribute 'items'

不确定如何更改我的代码,以便它与给定的测试代码一起使用。

2 个答案:

答案 0 :(得分:2)

看起来您在原始代码中发现了错误,因此您可能都会受到照顾。

也就是说,您可以使用collections.Counter()来加强代码。文档中的示例与您的作业非常匹配:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

答案 1 :(得分:0)

弄清楚我做错了什么。只需删除最后两行代码并返回计数字典。测试代码完成其余部分:)