Python debug——TypeError: unhashable type(list/set/dict)

正如错误提示,list/set/dict 均不可被哈希。

这一异常通常出现在,调用 set(…) 来构造一个 set (集合类型)时,set() 需要传递进来可哈希的元素(hashable items)。

    (1)list、set、dict:是不可哈希的

>>> list.__hash__
None
>>> set.__hash__
None
>>> dict.__hash__
None
  
  

    (2)int、float、str、tuple:是可以哈希的

    >>> int.__hash__
    <slot wrapper '__hash__' of 'int' objects>
    >>> float.__hash__
    <slot wrapper '__hash__' of 'float' objects>
    >>> str.__hash__
    <slot wrapper '__hash__' of 'str' objects>
    >>> tuple.__hash__
    <slot wrapper '__hash__' of 'tuple' objects>
      
      

      (3)list 不使用 hash 值进行索引,故其对所存储元素没有可哈希的要求;set / dict 使用 hash 值进行索引,也即其要求欲存储的元素有可哈希的要求。

      >>> set([[], [], []])
      TypeError: unhashable type: 'list'
      >>> set([{}, {}, {}])
      TypeError: unhashable type: 'dict'
      >>> set([set(), set(), set()])
      TypeError: unhashable type: 'set'
        
        

        (4)dict 仅对键(key)有可哈希的要求,对值(value)无此要求

        >>> dict([[["zhangsan", "lisi"], 20]])
        TypeError: unhashable type: 'list'
          
          

          注:可能你会问,set 不是可以接受 list,并将其转换为 set 吗?比如set([1, 2, 3]),注意,上文说的可哈希,不可哈希,是对可迭代类型(iterables)所存储元素(elements)的要求,[1, 2, 3]是可迭代类型,其存储元素的类型为int,是可哈希的,如果set([[1, 2], [3, 4]])[[1, 2], [3, 4]]list of lists(list 构成的 list)自然是可迭代的,但其元素为 [1, 2][3, 4]是不可哈希的。

          为什么 list 是不可哈希的,而 tuple 是可哈希的

          • (1)因为 list 是可变的在它的生命期内,你可以在任意时间改变其内的元素值。

          • (2)所谓元素可不可哈希,意味着是否使用 hash 进行索引

          • (3)list 不使用 hash 进行元素的索引,自然它对存储的元素有可哈希的要求;而 set 使用 hash 值进行索引。

          参考文献:
          1、https://blog.csdn.net/lanchunhui/article/details/50955238 2018.9.19
          2、https://stackoverflow.com/questions/6754102/typeerror-unhashable-type 2018.9.19

          猜你喜欢

          转载自blog.csdn.net/JohinieLi/article/details/82770780