新版: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的更多相关文章

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

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

  2. python set add 导致问题 TypeError: unhashable type: 'list'

    问题复现 >>> a = set() >>> b = set() >>> b.add(1) >>> a.add(b) Trace ...

  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. 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 ...

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

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

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

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

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

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

  9. 转:python中对list去重的多种方法

    对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ...

随机推荐

  1. os.path模块【python】

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  2. leetcode -- permutation 总结

    leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II

  3. Effective C++ —— 实现(五)

    条款26 : 尽可能延后变量定义式的出现时间 1. 你不只应该延后变量的定义,直到非得使用该变量的前一刻为止,甚至应该尝试延后这份定义直到能够给它初值实参为止.这样,不仅能够避免构造(和析构)非必要对 ...

  4. php学习六:字符串

    前言:越来越觉得php的强大之处了,不紧是数组,在字符串方面也可以看出它的优势,第一:方法多,集合了js,c,c#等多门语言的方法:第二:有许多方法是其他语言不具备的,如他的模糊比较,就是其他语言所没 ...

  5. EL表达式格式化日期时间

    1.首先引入标签库 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> ...

  6. 在lampp的proftpd下新增FTP用户的方法与配置

    用LAMPP的安装方法可以开一个默认的lampp用户,不过多用户怎样管理.目录怎样设置?这里简明说一下. 要求:使用Lampp的proftpd,开通多个FTP用户,并各分配一个目录,而且需要限制用户在 ...

  7. session.setAttribute和session.getAttribute

    网上搜了些资料 ----------------------------------------------------------------------------- B/S架构中,客户端与服务器 ...

  8. elk日志分析与发掘深入分析

    elk日志分析与挖掘深入分析 1 为什么要做日志采集? 2 挖财自己的日志采集和分析体系应该怎么建? 2.1 日志的采集 2.2 日志的汇总与过滤 2.3 日志的存储 2.4 日志的分析与查询 3 需 ...

  9. Android屏幕适配和文字屏幕适配

    http://blog.sina.com.cn/s/blog_9996c67e0101euwd.html 最近在一个项目中要实现屏幕适配平板和手机等不同的型号,而蛋疼的美工给了一套图,而且这些图纸有在 ...

  10. MySQL命令行基本命令操作

    进入命令模式后, 显示所有数据库 show databases; 选定某个数据库 use 数据库名; 创建数据库 create database 数据库名; 删除数据库 drop table 数据库名 ...