TypeError: unhashable type: 'dict', 当 dict 用作​​另一个 dict 的键时 [英] TypeError: unhashable type: 'dict', when dict used as a key for another dict

查看:73
本文介绍了TypeError: unhashable type: 'dict', 当 dict 用作​​另一个 dict 的键时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

 for json[referenceElement].keys() 中的元素:

当我运行该代码时,出现此错误:

<块引用>

TypeError: unhashable type: 'dict'

该错误的原因是什么,我可以做些什么来修复它?

解决方案

从错误中,我推断 referenceElement 是一个字典(参见下面的 repro).字典不能被散列,因此不能用作另一个字典的键(或者它本身!).

<预><代码>>>>d1, d2 = {}, {}>>>d1[d2] = 1回溯(最近一次调用最后一次):文件<input>",第 1 行,在 <module> 中类型错误:不可散列的类型:'dict'

您可能指的是 for element in referenceElement.keys()for element in json['referenceElement'].keys().有了关于 jsonreferenceElement 是什么类型以及它们包含什么内容的更多上下文,如果两种解决方案都不起作用,我们将能够更好地帮助您.

I have this piece of code:

for element in json[referenceElement].keys():

When I run that code, I get this error:

TypeError: unhashable type: 'dict'

What is the cause of that error and what can I do to fix it?

解决方案

From the error, I infer that referenceElement is a dictionary (see repro below). A dictionary cannot be hashed and therefore cannot be used as a key to another dictionary (or itself for that matter!).

>>> d1, d2 = {}, {}
>>> d1[d2] = 1
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'dict'

You probably meant either for element in referenceElement.keys() or for element in json['referenceElement'].keys(). With more context on what types json and referenceElement are and what they contain, we will be able to better help you if neither solution works.

这篇关于TypeError: unhashable type: 'dict', 当 dict 用作​​另一个 dict 的键时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆