字符串到字典的字数统计 [英] String to Dictionary Word Count

查看:154
本文介绍了字符串到字典的字数统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在做作业时遇到了麻烦.

So I'm having trouble with a homework question.

编写一个函数word_counter(input_str),它使用字符串input_str并返回一个字典,该字典将input_str中的单词映射到它们的出现次数.

Write a function word_counter(input_str) which takes a string input_str and returns a dictionary mapping words in input_str to their occurrence counts.

到目前为止,我的代码是:

So the code I have so far is:

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"))运行代码时,我得到的结果是:

Now when I run the code with a test case such as word_counter("This is a sentence") in the Python shell I get the result of:

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

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

Which is what is required. However, the test code that is used to check the answer is:

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

当我用该代码运行它时,出现错误:

And when I run it with that code I get the error:

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

不确定如何更改我的代码,使其与给定的测试代码一起使用.

Not sure how to change my code so that it works with the test code given.

推荐答案

您似乎在原始代码中发现了错误,因此您可能会受到照顾.

It looks like you found the error in the original code, so you may be all taken care of.

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

That said, you can tighten-up the code by using collections.Counter(). The example for it in the docs closely matches your assignment:

>>> # 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)]

这篇关于字符串到字典的字数统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆