【问题标题】:python dict is unhashable [duplicate]python dict是不可散列的[重复]
【发布时间】: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?现在很难说问题出在哪里,因为所有适用的代码都不存在。
  • 打印name1itemdescription1。至少其中一个是dict 类型,您正试图将其用作字典中的键,因此出现错误。
  • 简短问题:在像 {a: b} 这样的字典中,哪一个是关键?你怎么称呼另一个?谢谢

标签: python dictionary


【解决方案1】:

回答问题如前所述

任何未在其类定义中实现 __hash__()__eq()__ 魔术方法的对象都是不可散列的。

可散列对象是可以映射到唯一标识值的东西 - 考虑一个可以获取您的对象并返回相应唯一编号的函数,您的想法是正确的。它们是哈希表和字典的构建块,它们的工作前提是,键的这个唯一值(哈希)是指存储值的内存块——这样你就可以查找非常非常快的键(实际上是恒定时间)。

在实践中,这种不可散列条件通常意味着 可变对象 - 其内容可以在内存中更改的对象,而不是通过返回一个新的修改对象 - 是不可散列的。

列表和字典是不可散列的。元组、字符串和整数——所有不可变对象——都是可散列的。

可以扩展列表和字典,使其可散列。但是,只有少数小众用例可以证明使用可散列字典和列表是合理的。在我看来,这不是其中之一。


对实际基础问题的回答

Unhashable type 错误通常会在您使用字典 A 和不可散列的对象 B 并尝试使用 B 作为键(即调用 A[B])时引发。

您的错误仅仅是因为name1currentroomitemdescription1 中的一个是字典,而您试图在另一个字典中查找其中一个。

如果没有完整代码,我们无法确定其中哪一个是罪魁祸首,但您可以放心,错误来自那里。

【讨论】:

  • 很可能是itemdescription1。好像它是currentroom,OP 会收到错误“不支持的操作数 dict 和 int”(对于 currentroom -1)。而name1 本身就是字典中的一个键 (thisitem) 所以itemdescription1 应该是罪魁祸首。
猜你喜欢
  • 2021-11-21
  • 2015-08-23
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
相关资源
最近更新 更多