python使用set来去重碰到TypeError: unhashable type
新版:Python 的 unhashable type 错误分析及解决
python使用set来去重是一种常用的方法. 一般使用方法如下:
# int
a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print "orginal:", a
print list(set(a)) # str
a = [str(i) for i in a]
print "orginal:", a
print list(set(a))
某些情况会碰到类似这样的错误: TypeError: unhashable type: 'list'
list是可变类型, 无法进行hash, tuple就可以解决这个问题
# nested list
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0], [1, 2]]
print "orginal:", a
try:
print list(set(a)) # TypeError: unhashable type: 'list'
except TypeError, e:
print "Error:", e # tuple list
a = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0), (1, 2)]
print "orginal:", a
print list(set(a))
希望可以帮助碰到类似问题的人.
python使用set来去重碰到TypeError: unhashable type的更多相关文章
- python基础中遇到的问题(TypeError: unhashable type: 'list')
d20220330 #false >>> l=[{i:i+1} for i in [1,2,3]] >>> l [{1: 2}, {2: 3}, {3: 4}] & ...
- python set add 导致问题 TypeError: unhashable type: 'list'
问题复现 >>> a = set() >>> b = set() >>> b.add(1) >>> a.add(b) Trace ...
- 2. python提示:TypeError: unhashable type: 'list'
原因是,python字典的key不支持list类型和dict类型,需要转换 错误时 将list类型强制转换成string,用"".join(list). 修改后:
- Python3基础 list dict set 均为unhashable type
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...
- Python报错:TypeError: data type not understood
K-Means聚类算法 def randCent(dataSet, k): m, n = dataSet.shape # numpy中的shape函数的返回一个矩阵的规模,即是几行几列 centrod ...
- 诡异错误二:TypeError: data type not understood
如何使用Python产生一个数组,数组的长度为1024,数组的元素全为0? 很简单啊, 使用zeros(1024) 即可实现! 如何产生一个2×1024的全0矩阵呢?是否是zeros(2,1024) ...
- JQuery中button提交表单报TypeError: elem[type] is not a function jquery
错误: TypeError: elem[type] is not a function jquery 解决: 出现这种现象的原因是,提交的表单中,有标签的name,有以submit命名的 name中不 ...
- 转:python中对list去重的多种方法
对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ...
随机推荐
- len()
len() 用于统计序列的长度,字符串 .元组 .列表都属于序列 In [1]: str = "hello world" In [2]: len(str) Out[2]: 11
- NUC970烧录文件系统
燒錄U-Boot依照下列步驟將編譯完成的U-Boot燒錄至NAND Flash/SPI Flash/eMMC 中.U-Boot的編譯方法請參考4.3章節.3.11.1 燒錄所需檔案4. u-boot. ...
- [SQL]躺着也中枪的datetime类型
写在前面 本来这个东西,我是不想在这里总结的,今天有初学者的朋友问我了,那就不得不说说了,你肯定也踩过这样的坑,没遇到,说明你运气好,编码习惯好.那还是言归正传吧.避免你中枪,还是扫一眼这篇文章吧. ...
- mySQL数据库二:命令行的使用
在做整理的时候,上一篇刚开始只是简单的做了个数据类型的开头,在这里简单说一下mySQL的使用以及它的命令行 1.准备工作 有一个好的开发工具可以几何倍数的增加我们的工作效率,所以,工具是必不可少的,首 ...
- 计算从ios照片库中选取的图片文件大小
本文转载至:http://blog.csdn.net/longzs/article/details/8373586 从 iphone 的 照片库中选取的图片,由于 系统不能返回其文件的具体路径,所以这 ...
- eclipse导入maven项目时报Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources
在用Eclipse IDE for Java EE Developers进行maven项目的开发时,报错Could not calculate build plan: Plugin org.apach ...
- 《转载》POI导出excel日期格式
参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...
- 《转》python学习(8)元组
转自 http://www.cnblogs.com/BeginMan/p/3156235.html 一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这 ...
- Oracle存储过程--案例
限额控制 CREATE OR REPLACE PACKAGE BODY NP_PCKG_MERCHANT_LIMIT AS PROCEDURE CHECK_LIMIT ( in_iplCode IN ...
- 170713、springboot编程之多数据源切换
我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...