TypeError: unhashable type: 'list'

在使用list创建set的时候,一直出现TypeError: unhashable type: 'list',这种错误,检查代码,并没有出错,只可能是有些未知的错误,之前可能没有遇到过,百度后发现,原来是hash错误,就是list中的值不能hash。但是,set的创建也需要使用list,只能够在声明的时候直接使用list创建可以。

>>> s = set(range(1, 10))
>>> s
set([1, 2, 3, 4, 5, 6, 7, 8, 9])


>>> li = range(1, 11)
>>> li.append(range(1, 11))
>>> print li
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
>>> s = set(li)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

同样的,使用list和dict去创建dict的时候,同样会出现这种错误

>>> d={[] : "str", {}: "11"}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Python不支持dict的key为list或dict类型,因为list和dict类型是unhashable(不可哈希)的。

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值