简体   繁体   English

PYTHON困惑为什么将值附加到字典的for循环不起作用

[英]PYTHON Confused on why for loop for appending values into dictionary isn't working

I have a text file that includes 我有一个文本文件,其中包括

"Try not to become a man of success, but rather try to become a man of value. Look deep into nature, and then you will understand everything better. The true sign of intelligence is not knowledge but imagination. We cannot solve our problems with the same thinking we used when we created them. Weakness of attitude becomes weakness of character. You can't blame gravity for falling in love. The difference between stupidity and genius is that genius has its limits." “不要试图成为一个成功的人,而要当一个有价值的人。深入自然,然后你会更好地理解一切。智慧的真正标志不是知识,而是想象力。我们不能通过解决问题来解决问题创造它们时,我们使用了同样的想法。态度的弱化变成了品格的弱点。你不能责怪重力坠入爱河。愚蠢和天才的区别在于天才有其局限性。”

I am trying to put words as keys into a dictionary and have the line numbers they are in for the values. 我正在尝试将单词作为关键字放入字典中,并使用它们的值作为行号。

Here is my code and I don't know why it doesn't work 这是我的代码,我不知道为什么它不起作用

dictionary = dict()
    i = 0
    for line in fp:
        for word in line.lower().split():
            if word in dictionary:
                dictionary[word].append(i)
            else:
                dictionary[word] = i
        i = i + 1

This is the correction: 这是更正:

else:
    dictionary[word] = [i]

The first time you insert a word as a key, its value has to be a list, not an integer, so that the next time you find the same word you can append to list. 第一次插入单词作为键时,其值必须是列表,而不是整数,以便下次找到相同的单词时,可以将其附加到列表中。 But you can't append to a int. 但是您不能附加到int。

So here's the fixed code : 所以这是固定代码:

dictionary = dict()
i = 0
for line in fp:
    for word in line.lower().split():
        if word in dictionary:
            dictionary[word].append(i)
        else:
            dictionary[word] = [i]
    i = i + 1

A cleaner way to do it is to use a default dict with default value being a list. 一种更清洁的方法是使用默认字典,默认字典为列表。

from collections import defaultdict
dictionary = defaultdict(list)
for i,line in enumerate(fp):
    for word in line.lower().split():
            dictionary[word].append(i)

I think the general problem here is that a dictionary key have to be unique ... how many words you have in the sentence are repeated at least 2x? 我认为这里的普遍问题是字典键必须是唯一的...句子中有多少个单词至少重复2次?

list of repeated words: (try, not, to, become, a, man, of, but, and, you, the, is, we, weakness, genius) 重复单词列表:(尝试,而不是,成为,是,但是,而你,是,我们,弱点,天才)

then also words like "can't" will be split to "can" and "t" (do you want to handle t as not?) 然后还会将“不能”之类的单词分为“可以”和“ t”(您是否想不处理t?)

Example of what you are asking for: having 2x key:value pair of 'try': 1 您所要求的示例:具有2个key:value对的'try': 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM