【问题标题】:How to solve Key error in Jupytor notebook如何解决 Jupyter Notebook 中的 Key 错误
【发布时间】:2020-04-19 08:21:52
【问题描述】:

我看到一个使用Fruit360 dataset 的项目并尝试运行代码。我已导入所有库,但在编译代码时出错。你能帮我解决这个问题吗?

代码:

Test_label_ids = np.array([label_to_id_dict[x] for x in Test_labels])

错误信息:

KeyError                                  Traceback (most recent call last)
<ipython-input-15-c5a5039c5bdd> in <module>
----> 1 Test_label_ids = np.array([label_to_id_dict[x] for x in Test_labels])

<ipython-input-15-c5a5039c5bdd> in <listcomp>(.0)
----> 1 Test_label_ids = np.array([label_to_id_dict[x] for x in Test_labels])

KeyError: 'C:\\Users\\Msi\\Desktop\\KaggleDataset\\fruits-360_dataset\\fruits-360\\Test\\Apple Braeburn'

【问题讨论】:

  • Test_labels的内容是什么?

标签: python neural-network jupyter-notebook conv-neural-network


【解决方案1】:

通常情况下,在Python中,当你使用你创建的字典中不存在的键时会发生KeyError,你可以试试Following

from collections import defaultdict
ans=defaultdict(int)
#ans=defaultdict(default_factory)

当您使用 deafultdict 创建字典时 它永远不会产生密钥错误 当您使用 int 作为默认工厂时,它将输出为零, 如果您使用列表而不是 int,它将返回一个空列表。

【讨论】:

  • 感谢您的回复。我尝试实现代码,但它抛出了同样的错误。
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 2020-05-15
  • 2022-01-10
  • 1970-01-01
  • 2022-07-12
  • 2020-06-20
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多