即使密钥在字典中,Python也会引发KeyError(对于字典外的密钥)

2024-04-28 17:24:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了字典外键的一个键错误,尽管我知道键实际上在字典中。有什么可能导致这种情况的想法吗?

print G.keys()

返回以下内容:

['24', '25', '20', '21', '22', '23', '1', '3', '2', '5', '4', '7', '6', '9', '8', '11', '10', '13', '12', '15', '14', '17', '16', '19', '18']

但当我试图访问下一行代码的字典中的值时。。。

for w in G[v]:    #note that in this example, v = 17

我收到以下错误消息:

KeyError: 17

任何帮助、提示或建议都是值得赞赏的。谢谢。


Tags: 代码in消息for字典thatexample错误