【发布时间】:2016-04-29 04:18:02
【问题描述】:
这里是新的 python 学习者,我遇到了一个问题,它说我的 dict 是不可散列的。我在堆栈溢出中搜索了这个以获取其他答案,但我无法很好地理解它们。有没有人对什么是不可散列的东西有一个易于理解的解释?这也是我的代码中出现问题的部分
for name1, itemdescription1 in thisitem.items():
if rooms[currentroom-1][currentroom]["items"][name1][itemdescription1] == "yes": # if player already got this item
print ("You didn't find anything")
在上面代码的中间行它说 TypeError: unhashable type: 'dict' 这是我的代码
rooms = [
{1 : {
"name" : "hall",
"east" : 2,
"south" : 3,
"description" : "An empty hallway, nothing of use here. ",
"items" : {"torch" : {"a dim torch " : "no"}}
}},
{2 : {
"name" : "kitchen",
"west" : 1,
"east" : 4,
"south" : 6,
"description" : "An odd smell reeks from the kitchen. What could it be? ",
"items" : {"meat" : {"a piece of vile meat " : "no"}}
}},
{3 : {
"name" : "bedroom",
"north" : 1,
"east" : 6,
"description" : "A overwelmingly big room containing two gigantic beds, hmmm... ",
"items" : {"key" : {"a silver key " : "no"}}
}},
{4 : {
"name" : "diningroom",
"west" : 2,
"south" : 5,
"description" : "A large room with tables and chairs, nothing special. ",
"items" : {"plate" : {"a white plate " : "no"}}
}},
{5 : {
"name" : "bathroom",
"north" : 4,
"west" : 6,
"description" : "A creepy shadow lays in the bathtub, better go somewhere else. ",
"items" : {"shampoo" : {"a bottle of shampoo " : "no"}}
}},
{6 : {
"name" : "garage",
"north" : 2,
"west" : 3,
"east" : 5,
"description" : "It reeks of blood here. You wonder why. ",
"items" : {"bolts" : {"some rusted iron bolts " : "no"}}
}}
]
【问题讨论】:
-
thisitem.items()中的值是什么? -
您能否为您的问题创建一个Minimum, Complete and Verifiable Example?现在很难说问题出在哪里,因为所有适用的代码都不存在。
-
打印
name1和itemdescription1。至少其中一个是dict类型,您正试图将其用作字典中的键,因此出现错误。 -
简短问题:在像 {a: b} 这样的字典中,哪一个是关键?你怎么称呼另一个?谢谢
标签: python dictionary