简体   繁体   English

计算列表中的单词并将它们添加到字典中,以及出现次数 Python

[英]Count words in a list and add them to a dictionary, along with number of occurrences Python

I'll type the question my code is in response to below for clarity, but I'm having 2 issues.为了清楚起见,我将在下面输入我的代码要回答的问题,但我遇到了 2 个问题。 It seems to be adding correctly up to a point, and the result for the count of the other words in the sentence seems to be accurate, but rabbit suddenly jumps from 1 to 4 and I'm not sure why.它似乎正确地添加到一个点,并且句子中其他单词的计数结果似乎是准确的,但是兔子突然从 1 跳到 4,我不知道为什么。

I'm also getting this: Error: AttributeError: 'int' object has no attribute 'items'我也得到这个:错误:AttributeError:'int' object has no attribute 'items'

Here is the problem followed by my code.这是我的代码后面的问题。 Thanks!谢谢!

Provided is a string saved to the variable name sentence.提供的是保存到变量名句中的字符串。 Split the string into a list of words, then create a dictionary that contains each word and the number of times it occurs.将字符串拆分为单词列表,然后创建一个包含每个单词及其出现次数的字典。 Save this dictionary to the variable name word_counts.将此字典保存到变量名 word_counts。

sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."
sentence_case = sentence.lower()
sentence_list = sentence_case.split()
sentence_dictionary = {}
word_counts = 0

for item in sentence_list:
    if item in sentence_dictionary:
        word_counts += 1
        sentence_dictionary[item] = word_counts

    else:
        sentence_dictionary[item] = 1

If, i understand you right you might remove the word_count variable to count the frequency of words如果,我理解你的权利,你可以删除 word_count 变量来计算单词的频率

sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."
sentence_case = sentence.lower()
sentence_list = sentence_case.split()
sentence_dictionary = {}

for item in sentence_list:
    if item in sentence_dictionary:
        sentence_dictionary[item] += 1

    else:
        sentence_dictionary[item] = 1

print(sentence_dictionary)

if you want to save that in word_counts you can make it like that:如果你想把它保存在 word_counts 中,你可以这样:

word_counts = sentence_dictionary

I hope i could help you我希望我能帮助你

Try this尝试这个

sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."
sentence_case = sentence.lower()
sentence_list = sentence_case.split()
sentence_dictionary = {}

for item in sentence_list:
    if item in sentence_dictionary:
        sentence_dictionary[item] += 1

    else:
        sentence_dictionary[item] = 1

What is the purpose behind the variable word_counts ?变量word_counts背后的目的是什么? The current usage of this variable conflates the counts of different words.此变量的当前使用将不同单词的计数混为一谈。 Your problem now reduces to having different counters for each word.您的问题现在减少到为每个单词设置不同的计数器。 Fortunately, you don't need to do this explicitly since sentence_dictionary is a set of counters on its own:) Just increment sentence_dictionary[item] under your if block and get rid of word_counts .幸运的是,您不需要显式执行此操作,因为sentence_dictionary本身就是一组计数器:) 只需在 if 块下增加 sentence_dictionary[item] 并去掉word_counts

As a sidenote, this is a good example to leverage the defaultdict class provided by Python.作为旁注,这是利用Python提供的默认字典 class 的一个很好的例子。 defaultdict is initialized along with a default_factory as its argument. defaultdict 与 default_factory 作为其参数一起初始化。 This allows you to initialize dictionaries of counts, lists, sets, etc. without explicitly handling edge cases.这允许您初始化计数、列表、集合等的字典,而无需显式处理边缘情况。

Consider the code sample below:考虑下面的代码示例:

dic = defaultdict(int)
dic[0] += 1
dic[0] # prints 1

If you notice, I didn't have to explicitly check if the key was present in the dictionary since the defaultdict class handles this under the hood by automatically creating the key with value 0 (value 0 because the default_factory is int()).如果您注意到,我不必显式检查该键是否存在于字典中,因为 defaultdict class 通过自动创建值为 0 的键(值为 0,因为 default_factory 是 int())来处理这个问题。 You can explore the Python docs to understand how defaultdict works.您可以浏览 Python 文档以了解 defaultdict 的工作原理。 It will save you some time and debugging in the future!它将为您节省一些时间和将来的调试!

You can try this,it worked for me.你可以试试这个,它对我有用。

sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."
sentence_list = sentence.split()
sentence_dictionary = {}

for item in sentence_list:
    if item not in sentence_dictionary:
        sentence_dictionary[item] = 0
    sentence_dictionary[item] += 1
word_counts = sentence_dictionary
print(word_counts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算值字典列表python的出现次数 - Count the number of occurrences for values dictionary list python 计算python字典中某个值的出现次数? - count the number of occurrences of a certain value in a dictionary in python? 从列表中计算字典中的单词数 - Count the number of words in dictionary from a list 计算python列表中标记之间的出现次数 - Count the number of occurrences between markers in a python list Python-如何计算列表中的出现次数 - Python - How to count the number of occurrences in a list Python:计算字符串中列表项的出现次数 - Python: Count number of occurrences of list items in a string Python-计算列表中日期的出现次数 - Python- Count number of occurrences of a date in a list 计算Python列表中出现次数的最快方法 - Fastest way to count number of occurrences in a Python list Python 从字典中拆分列表中的字符串后,分别计算出现次数以及出现次数最多的元素 - Python count the number of occurrences and also the elements with maximum occurences separately after splitting the string in list from dictionary 如何计算python中字典中所有值出现的次数? TypeError:无法散列的类型:'list' - How count the number of occurrences of all the values in a dictionary in python? TypeError: unhashable type: 'list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM