新版: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. 使用NSTimer实现动画

    1.新建empty AppLication,添加HomeViewController页面, iphone.png图片 2.在 HomeViewController.xib中添加Image View,并 ...

  2. Oracle分页查询sql语句

    1. select * from ( select  t.*, rownum RN from TABLE_NAME  t ) where RN > 0 and RN <= 15 2. se ...

  3. MITE

    .,text/html .,text/h323 .aaf,application/octet-stream .aca,application/octet-stream .accdb,applicati ...

  4. Computer Science Theory for the Information Age-5: 学习理论——VC维的定义以及一些例子

    学习理论——VC维的定义以及一些例子 本文主要介绍一些学习理论上的东西.首先,我们得明确,从训练集上学习出来的分类器的最终目标是用于预测未知的样本,那么我们在训练的时候该用多少的样本才能使产生的分类器 ...

  5. win8.1简单快速安装phpnow的方法

    工具/原料 phpnow 1.5.6 管理员身份登陆系统 方法/步骤   下载phpnow 这是必须的,大家可以自行百度下载,然后我们将phpnow放到一个文件夹,可以是根目录,也可以不是,但一定要知 ...

  6. LeetCode——Number of 1 Bits

    //求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...

  7. 【APIO2016】Fireworks[DP 可并堆维护凸包优化]

    4585: [Apio2016]烟火表演 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 100  Solved: 66[Submit][Status] ...

  8. c# SQL Server数据库操作-数据适配器类:SqlDataAdapter

    SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...

  9. 在ScrollView嵌套GridView,使GridView不滚动

    <ScrollView>       ……   <LinearLayout> </LinearLayout>       ……</ScrollView> ...

  10. jQuery Ajax 全解析(转载)

    本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 转载请标明出处! jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写Java ...