0
Follow
0
View

How to solve the problem of #keyerro#?

dongguangshuai 注册会员
2023-02-28 04:30

Here is the corrected code:

list = {}
for ks in key_sentences:
    words = T.postag(ks)
    for w in words:
        if w.flag == "nr":
            if w.word not in target_list:
                target_list[w.word] = 1
            else:
                target_list[w.word] += 1

If the answer is helpful to you, please accept

xuhui820407 注册会员
2023-02-28 04:30

Modify the simplified code as follows :

list = {}
for ks in key_sentences:
    # print ks
    words = T.postag(ks)
    for w in words:
        # print "====="
        # print w.word
        if w.flag == "nr":
            target_list[w.word] = target_list.get(w.word, 0) + 1


gsdhdsz 注册会员
2023-02-28 04:30
if w.word not in list:
               target_list[w.word] += 1

The w.ord is not in the target_list, so it cannot +1.
target_list[w.ord] = 1

liwandong0612 注册会员
2023-02-28 04:30

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > this code takes a KeyError is unusual, the error message shows the" KeyError: 'the abbot." This error is usually caused by using an invalid Key in Python.

Specifically, the code uses a dictionary object, list, to hold some statistics, where the key is some person's name and the value is the number of times that person's name appears in the text. When the program executes to the line "target_list[w.ord] += 1", Python tries to access the element in the target_list dictionary with the key w.ord and increments its value by 1. However, if w.ord does not exist in the target_list dictionary, Python will raise a KeyError indicating that the key does not exist.

According to the error message, you can see that the KeyError is "Shi Yongxin". It is possible that the key did not exist because it was incorrectly added to the target_list dictionary in the previous code. The solution to this problem is to add appropriate logic to your code to ensure that the key to be accessed exists in the dictionary, or to add the key to the dictionary before accessing it. For example, you could add the following logic to your code:

if w.word not in target_list:
    target_list[w.word] = 1
else:
    target_list[w.word] += 1


Thus, if w.ord is not in the target_list dictionary, it is first added to the dictionary and set to 1; Otherwise, simply increase its value by 1.

sync3939 注册会员
2023-02-28 04:30

This error was caused by a KeyError when you tried to increment the value of the w.ord item in the list dictionary, which does not exist. You can avoid this by adding judgments to your code, such as

target_list = {}
for ks in key_sentences:
    words = T.postag(ks)
    for w in words:
        if w.flag == "nr":
            if w.word not in target_list:
                target_list[w.word] = 1
            else:
                target_list[w.word] += 1

Thus, during the traversal of words, if w.ord is not in the target_list, a new entry with the key w.ord will be created with the value 1. If it already exists, increment its value by one. This avoids KeyError exceptions.

xye20070408 注册会员
2023-02-28 04:30

Your code's judgment is inverted. It should actually be =1 when not in the list and +=1 when in the list.

                    if w.word not in target_list:
                        target_list[w.word] = 1
                    else:
                        target_list[w.word] += 1
cxc02325 注册会员
2023-02-28 04:30

or you can change from not in to in, in short, your condition and your calculation is not matched, just misplaced

dapi890621 注册会员
2023-02-28 04:30

This answer quotes ChatGPT

In Python, KeyError means trying to access a key that does not exist in the dictionary, that is, accessing a key that does not exist raises the exception. In code, KeyError: 'Shi Yongxin' indicates that there is no key named 'Shi Yongxin' in the dictionary list, but a KeyError is raised by trying to use it for an increment operation.

From the code logic, you can see that the exception is thrown on the following line of code:

if w.word not in target_list:
    target_list[w.word] += 1
else:
    target_list[w.word] = 1

uses the dictionary's += operator here, and raises a KeyError if the key is not in the dictionary. To avoid this exception, you can use the if-else statement or the dictionary's get() method to determine whether the key exists. Modify the code as follows:

if w.word not in target_list:
    target_list[w.word] = 1
else:
    target_list[w.word] += 1

or use the dictionary get() method:


target_list[w.word] = target_list.get(w.word, 0) + 1

All of the above changes initialize the specified key to 1 or 0 if it does not exist in the dictionary, and then increments it. This avoids KeyError exceptions.

ctc1027 注册会员
2023-02-28 04:30

if w.word not in list:
                        target_list[w.word] += 1

+= instead of =, you don't even exist, so of course you can't +=

About the Author

Question Info

Publish Time
2023-02-28 04:30
Update Time
2023-02-28 04:30