类型错误 python 不可散列的类型
type error python unhashable type
我正在尝试找出 dict1 和 dict2 之间的区别,但我总是收到错误任何帮助?
ret = {}
third_value_list =[0,1]
for i in third_value_list:
#print i
num_list = [1,2]
val_list = [0,1]
dict1 = dict((k, [v]+[i]) for (k, v) in zip(num_list,val_list))
print dict1
num_list2= [1,2]
val_list2 = [0,6]
dict2 = dict((k, [v]+[i]) for (k, v) in zip(num_list2,val_list2))
print dict2
if set(dict2.items()) - set(dict1.items()):
print 'true'
a = set(dict1.items()) - set(dict2.items())
ret.update (a)
print ret
输出:
{1: [0, 0], 2: [1, 0]}
回溯(最近调用最后):
文件 "C:\Randstad-ISS\workspace\pattern2\src\pat2\t4.py",第 46 行,在
if set(dict2.items()) - set(dict1.items()):TypeError: unhashable 类型: 'list'
{1: [0, 0], 2: [6, 0]}
{1: [0, 1], 2: [1, 1]}
{1: [0, 1], 2: [6, 1]}
为了将对象添加到集合中,它需要 hashable。只有不可变对象是可哈希的,因为 dict1
包含可变列表,所以你会得到错误。
来自 Python 文档:
An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() or cmp() method). Hashable objects which compare equal must have the same hash value.
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal (except with themselves), and their hash value is derived from their id().
错误发生在set(dict2.items())
。您正在尝试将 (1, [0,1])
和 (2, [1,1])
(这些是字典中的 "items")放入一个集合中。要放入集合中,需要对项目进行哈希处理。它们无法被散列,因为它们包含一个列表。列表是不可散列的,因为它可以更改,列表是可变的。只能对不可变对象进行哈希处理。
列表的不可变版本是元组。元组本质上是一个无法更改的列表。常见数据类型还有其他不可变版本,例如 frozensets 而不是 sets。
将这些列表更改为元组,您就可以对它们进行哈希处理了!
试试这个代码。主要思想是将dict1和dict2中的[v]+[i]
值转成元组,然后计算dict1和dict2的差值。最后,将元组类型值转换回列表。
ret = {}
third_value_list =[0,1]
for i in third_value_list:
#print i
num_list = [1,2]
val_list = [0,1]
dict1 = dict((k, tuple([v]+[i])) for (k, v) in zip(num_list,val_list))
print dict1
num_list2= [1,2]
val_list2 = [0,6]
dict2 = dict((k, tuple([v]+[i])) for (k, v) in zip(num_list2,val_list2))
print dict2
if set(dict2.items()) - set(dict1.items()):
print 'true'
a = dict(set(dict1.items()) - set(dict2.items()))
a = dict((k, [i for i in v]) for (k, v) in zip(a.keys(), a.values()))
ret.update (a)
print ret
我正在尝试找出 dict1 和 dict2 之间的区别,但我总是收到错误任何帮助?
ret = {}
third_value_list =[0,1]
for i in third_value_list:
#print i
num_list = [1,2]
val_list = [0,1]
dict1 = dict((k, [v]+[i]) for (k, v) in zip(num_list,val_list))
print dict1
num_list2= [1,2]
val_list2 = [0,6]
dict2 = dict((k, [v]+[i]) for (k, v) in zip(num_list2,val_list2))
print dict2
if set(dict2.items()) - set(dict1.items()):
print 'true'
a = set(dict1.items()) - set(dict2.items())
ret.update (a)
print ret
输出:
{1: [0, 0], 2: [1, 0]}
回溯(最近调用最后):
文件 "C:\Randstad-ISS\workspace\pattern2\src\pat2\t4.py",第 46 行,在
if set(dict2.items()) - set(dict1.items()):TypeError: unhashable 类型: 'list'
{1: [0, 0], 2: [6, 0]}
{1: [0, 1], 2: [1, 1]}
{1: [0, 1], 2: [6, 1]}
为了将对象添加到集合中,它需要 hashable。只有不可变对象是可哈希的,因为 dict1
包含可变列表,所以你会得到错误。
来自 Python 文档:
An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() or cmp() method). Hashable objects which compare equal must have the same hash value.
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal (except with themselves), and their hash value is derived from their id().
错误发生在set(dict2.items())
。您正在尝试将 (1, [0,1])
和 (2, [1,1])
(这些是字典中的 "items")放入一个集合中。要放入集合中,需要对项目进行哈希处理。它们无法被散列,因为它们包含一个列表。列表是不可散列的,因为它可以更改,列表是可变的。只能对不可变对象进行哈希处理。
列表的不可变版本是元组。元组本质上是一个无法更改的列表。常见数据类型还有其他不可变版本,例如 frozensets 而不是 sets。
将这些列表更改为元组,您就可以对它们进行哈希处理了!
试试这个代码。主要思想是将dict1和dict2中的[v]+[i]
值转成元组,然后计算dict1和dict2的差值。最后,将元组类型值转换回列表。
ret = {}
third_value_list =[0,1]
for i in third_value_list:
#print i
num_list = [1,2]
val_list = [0,1]
dict1 = dict((k, tuple([v]+[i])) for (k, v) in zip(num_list,val_list))
print dict1
num_list2= [1,2]
val_list2 = [0,6]
dict2 = dict((k, tuple([v]+[i])) for (k, v) in zip(num_list2,val_list2))
print dict2
if set(dict2.items()) - set(dict1.items()):
print 'true'
a = dict(set(dict1.items()) - set(dict2.items()))
a = dict((k, [i for i in v]) for (k, v) in zip(a.keys(), a.values()))
ret.update (a)
print ret