TypeError:不可用的类型:'dict',当字典用作另一个字典的关键字时 [英] TypeError: unhashable type: 'dict', when dict used as a key for another dict

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

问题描述

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

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


TypeError:不可用类型:'dict'

错误的原因是什么?解决方案

解决方案

从错误中,我推断出 referenceElement 是一本字典(参见下面的代码)。字典不能被散列,因此不能用作另一个字典(或其本身!)的关键字。

 > ;>> d1,d2 = {},{} 
>>> d1 [d2] = 1
Traceback(最近一次调用最后一次):
在< module>中,第1行的文件< input>
TypeError:unhashable类型:'dict'

您可能意指对于referenceElement.keys()中的元素或者来表示json ['referenceElement'] .keys()中的元素。通过更多关于 json referenceElement 类型的内容以及它们包含的内容,我们将能够更好地帮助您这两个解决方案都无效。


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:不可用的类型:'dict',当字典用作另一个字典的关键字时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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