python列表作为dict键

时间:2017-02-01 05:50:52

标签: python dictionary key

我有以下内容: 我看到以下对象具有str.split # ["three", "twot", "1three", "one", "three"] str.split.map { |word| number_map[word.to_sym] } # [3, nil, nil, 1, 3] 属性

__hash__

问:即使它具有a=[1,2,3] >>> dir(a) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__' , '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' ] >>> dict1={a:'55'} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list' 属性,仍然不能用作密钥。为什么?

1 个答案:

答案 0 :(得分:3)

您是否尝试查看属性的

>>> print(a.__hash__)
None

仅仅因为具有某个__hash__属性并不意味着它可以播放; __hash__属性必须是可调用的,实际上是哈希值(而不是例如引发异常)。