问题复现

>>> a = set()
>>> b = set() >>> b.add(1)
>>> a.add(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> c = list(b)
>>> a.add(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list' >>> tuple(b)
(1,)
>>> a.add(b)
>>>

现象:往set对象里add列表、集合对象时,时提示他们是不可hash的,而对于tuple类型就可以。

原因:set里面的对象是hash存储(所以是无序的),对于python万物都是对象,如果存储一个list对象,而后改变了list对象,那set中刚才存储的值的hash就变了。

结论:set是hash存储,必须存储不变的对象,例如字符串、数字、元组等。

python set add 导致问题 TypeError: unhashable type: 'list'的更多相关文章

  1. python使用set来去重碰到TypeError: unhashable type

    新版:Python 的 unhashable type 错误分析及解决 python使用set来去重是一种常用的方法. 一般使用方法如下: # int a = [1, 2, 3, 4, 5, 1, 2 ...

  2. python基础中遇到的问题(TypeError: unhashable type: 'list')

    d20220330 #false >>> l=[{i:i+1} for i in [1,2,3]] >>> l [{1: 2}, {2: 3}, {3: 4}] & ...

  3. 2. python提示:TypeError: unhashable type: 'list'

    原因是,python字典的key不支持list类型和dict类型,需要转换 错误时 将list类型强制转换成string,用"".join(list). 修改后:

  4. Python3基础 list dict set 均为unhashable type

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  5. Python报错:TypeError: data type not understood

    K-Means聚类算法 def randCent(dataSet, k): m, n = dataSet.shape # numpy中的shape函数的返回一个矩阵的规模,即是几行几列 centrod ...

  6. 诡异错误二:TypeError: data type not understood

    如何使用Python产生一个数组,数组的长度为1024,数组的元素全为0? 很简单啊, 使用zeros(1024) 即可实现! 如何产生一个2×1024的全0矩阵呢?是否是zeros(2,1024) ...

  7. arcgis python arcpy add data script添加数据脚本

    arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...

  8. JQuery中button提交表单报TypeError: elem[type] is not a function jquery

    错误: TypeError: elem[type] is not a function jquery 解决: 出现这种现象的原因是,提交的表单中,有标签的name,有以submit命名的 name中不 ...

  9. Extensions can add new functionality to a type, but they cannot override existing functionality.

    Extensions can add new functionality to a type, but they cannot override existing functionality.

随机推荐

  1. JavaWEB中读取配置信息

    第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...

  2. FZU 1025 状压dp 摆砖块

    云峰菌曾经提到过的黄老师过去讲课时的摆砖块 那时百度了一下题目 想了想并没有想好怎么dp 就扔了 这两天想补动态规划知识 就去FZU做专题 然后又碰到了 就认真的想并且去做了 dp思想都在代码注释里 ...

  3. Twitter Storm中Bolt消息传递路径之源码解读

    本文初次发表于storm-cn的google groups中,现以blog的方式再次发表,表明本人徽沪一郎确实读过这些代码,:). Bolt作为task被executor执行,而executor是一个 ...

  4. PHP 设计模式 笔记与总结(4)PHP 链式操作的实现

    PHP 链式操作的实现 $db->where()->limit()->order(); 在 Common 下创建 Database.php. 链式操作最核心的地方在于:在方法的最后 ...

  5. java字符串练习题

  6. json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。

    json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...

  7. Error when loading the SDK:

    android环境搭建:http://www.cnblogs.com/xdp-gacl/p/4322165.html eclipse启动,报了错.  解决: 从安装的android-sdk目录下的to ...

  8. redis 应用场景

    1.string类型 : 图片和视频文件,静态文件 2.list 双向链表:回帖ID,我的关注列表,消息队列 length = redis.lpush('users:newest', 'user:go ...

  9. awk统计nginx日志访问前一百的ip

    访问ip  awk '{print $1}' access.log| sort | uniq -c | sort -n -k 1 -r | head -n 100 访问地址 awk '{print $ ...

  10. hive中关于数据库与表等的基本操作

    一:基本用法 1.新建数据库 2.删除数据库 3.删除非空的数据库 4.指定数据库的位置 LOCATION:指定数据库的位置,不会在系统的默认文件下. 5.在指定数据库中新建表(验证在指定的数据库中可 ...