TypeError: unhashable type: 'list'

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

[python]  view plain  copy
  1. >>> s = set(range(110))  
  2. >>> s  
  3. set([123456789])  


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

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

[python]  view plain  copy
  1. >>> d={[] : "str", {}: "11"}  
  2. Traceback (most recent call last):  
  3.   File "<stdin>", line 1in <module>  
  4. TypeError: unhashable type: 'list'  

Python不支持dict的key为list或dict类型,因为list和dict类型是unhashable(不可哈希)的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值