Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关键错误Python_Python_Python 3.x - Fatal编程技术网

关键错误Python

关键错误Python,python,python-3.x,Python,Python 3.x,我想把所有的单词和它的标签都记在字典里。然而,我一直得到一个键错误,我不明白为什么 sent = [[('Merger', 'NOUN'), ('proposed', 'VERB')], [('Wards', 'NOUN'), ('protected', 'VERB')]] dicts = {} for x in sent: for y in x: if y[0] in dicts.keys(): dicts[y[0]][y[1]] = 1

我想把所有的单词和它的标签都记在字典里。然而,我一直得到一个键错误,我不明白为什么

sent = [[('Merger', 'NOUN'), ('proposed', 'VERB')], [('Wards', 'NOUN'), ('protected', 'VERB')]]

dicts = {}

for x in sent:
    for y in x:
        if y[0] in dicts.keys():
            dicts[y[0]][y[1]] = 1
        else:
            dicts[y[0]][y[1]] += 1
错误:

   KeyError               Traceback (most recent call last)
   <ipython-input-19-17c6695bd911> in <module>()
   17             dicts[y[0]][y[1]] = 1
   18         else:
---> 19             dicts[y[0]][y[1]] += 1

   KeyError: 'Merger'
keyrerror回溯(最近一次调用)
在()
17条指令[y[0]][y[1]]=1
18.其他:
--->19条指令[y[0]][y[1]]+=1
关键错误:“合并”

你把条件句搞错了。首先要检查字典中是否存在该键——如果不存在,则创建该键。那么,你嵌套得太远了。您只需要
dicts[y[0]]

有一个简单的解决方法:在dicts.keys()中的
之前添加
而不是
,然后去掉
[y[1]]

全文:

for x in sent:
    for y in x:
        if y[0] not in dicts.keys():
            dicts[y[0]] = 1
        else:
            dicts[y[0]] += 1

你把条件句搞错了。首先要检查字典中是否存在该键——如果不存在,则创建该键。那么,你嵌套得太远了。您只需要
dicts[y[0]]

有一个简单的解决方法:在dicts.keys()中的
之前添加
而不是
,然后去掉
[y[1]]

全文:

for x in sent:
    for y in x:
        if y[0] not in dicts.keys():
            dicts[y[0]] = 1
        else:
            dicts[y[0]] += 1

<>你也应该考虑看和:

defaultdict
将自动填充默认值,
计数器
是专门用于计数的
dict

from collections import defaultdict
from collections import Counter

sent = [[('Merger', 'NOUN'), ('proposed', 'VERB')], [('Wards', 'NOUN'), ('protected', 'VERB')]]

dicts = defaultdict(Counter)  # A default dictionary of Counters
for x in sent:
    for y in x:
        dicts[y[0]][y[1]] += 1

print(dicts)
# defaultdict(<class 'collections.Counter'>, {'Merger': Counter({'NOUN': 1}), 'proposed': Counter({'VERB': 1}), 'Wards': Counter({'NOUN': 1}), 'protected': Counter({'VERB': 1})})

<>你也应该考虑看和:

defaultdict
将自动填充默认值,
计数器
是专门用于计数的
dict

from collections import defaultdict
from collections import Counter

sent = [[('Merger', 'NOUN'), ('proposed', 'VERB')], [('Wards', 'NOUN'), ('protected', 'VERB')]]

dicts = defaultdict(Counter)  # A default dictionary of Counters
for x in sent:
    for y in x:
        dicts[y[0]][y[1]] += 1

print(dicts)
# defaultdict(<class 'collections.Counter'>, {'Merger': Counter({'NOUN': 1}), 'proposed': Counter({'VERB': 1}), 'Wards': Counter({'NOUN': 1}), 'protected': Counter({'VERB': 1})})

你从高级代表那里得到了两个快速的答案,但我很难理解
dicts[y[0]][y[1]]+=1
的有用性。您希望得到什么输出?@roganjosh hi我正在尝试创建一个嵌套字典。所以它将是{合并:{名词:1}}我想它实际上是用TerryA编辑的答案解决的。它只是看起来不对劲,我想知道是否有什么迂回的事情发生了。您试图引用嵌套在2深的dicts,而不首先创建内部dicts。Python不知道外部dict中的那些元素是字典。@PaulRooney嗨,我该怎么做你从高级代表那里得到了两个快速的答案,但我正在努力理解
dict[y[0]][y[1]+=1
的有用性。您希望得到什么输出?@roganjosh hi我正在尝试创建一个嵌套字典。所以它将是{合并:{名词:1}}我想它实际上是用TerryA编辑的答案解决的。它只是看起来不对劲,我想知道是否有什么迂回的事情发生了。您试图引用嵌套在2深的dicts,而不首先创建内部dicts。Python不知道外部dict中的那些元素是字典。@PaulRooney嗨,我该怎么做呢?这和我将要发布的内容差不多,所以我不会重复。通过在
中为元组解包,可以避免对元组的丑陋索引访问。e、 g.
对于名称,键入x:
与我将要发布的内容基本相同,因此我不会重复。通过在
中为元组解包,可以避免对元组的丑陋索引访问。e、 g.
对于名称,在x中键入: