问题描述
我有这段代码:
for json[referenceElement].keys() 中的元素:
当我运行该代码时,出现此错误:
TypeError: unhashable type: 'dict'
该错误的原因是什么,我可以做些什么来修复它?
从错误中,我推断 referenceElement
是一个字典(参见下面的 repro).字典不能被散列,因此不能用作另一个字典的键(或者它本身!).
您可能指的是 for element in referenceElement.keys()
或 for element in 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:
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 的键时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!