简体   繁体   English

类型错误python unhashable类型

[英]type error python unhashable type

I am trying to get the difference between dict1 and dict2 but i keep getting error any help? 我试图得到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

Outputs: 输出:

{1: [0, 0], 2: [1, 0]} {1:[0,0],2:[1,0]}

Traceback (most recent call last): 追溯(最近一次通话):

File "C:\\Randstad-ISS\\workspace\\pattern2\\src\\pat2\\t4.py", line 46,in 文件“ C:\\ Randstad-ISS \\ workspace \\ pattern2 \\ src \\ pat2 \\ t4.py”,第46行,在

if set(dict2.items()) - set(dict1.items()):TypeError: unhashable type: 'list' 如果set(dict2.items())-set(dict1.items()):TypeError:无法散列的类型:'列表'

{1: [0, 0], 2: [6, 0]} {1:[0,0],2:[6,0]}

{1: [0, 1], 2: [1, 1]} {1:[0,1],2:[1,1]}

{1: [0, 1], 2: [6, 1]} {1:[0,1],2:[6,1]}

In order to add the object to the set it needs to be hashable . 为了将对象添加到集合中,它需要是可哈希的 Only immutable objects are hashable and since dict1 contains lists which are mutable you get the error. 只有不可变的对象才是可dict1并且由于dict1包含可变的列表,因此会出现错误。

From Python documentation: 从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). 如果对象的哈希值在其生命周期内始终不变(需要使用hash ()方法),并且可以与其他对象进行比较(需要使用eq ()或cmp ()方法),则该对象是可哈希的。 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. 散列性使对象可用作字典键和set成员,因为这些数据结构在内部使用散列值。

All of Python's immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Python的所有不可变内置对象都是可哈希的,而没有可变容器(例如列表或字典)是可哈希的。 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(). 它们都比较不相等(除了它们本身),并且它们的哈希值是从其id()派生的。

The error happens in set(dict2.items()) . 该错误发生在set(dict2.items()) You are trying to place (1, [0,1]) and (2, [1,1]) (These are the the "items" in the dictionary) into a set. 您试图将(1, [0,1])(2, [1,1]) (这些是字典中的“项目”)放置到集合中。 To be placed into the set the items need to be hashed. 要放入集合中,需要对项目进行哈希处理。 They are unable to be hashed because they contain a list. 无法将它们散列,因为它们包含一个列表。 A list is unhashable because it can be changed, a list is mutable. 列表不可哈希,因为它可以更改,列表是可变的。 Only immutable objects can be hashed. 只能对不可变的对象进行哈希处理。

The immutable version of a list is a tuple. 列表的不可变版本是元组。 A tuple is essentially a list that cannot be changed. 元组本质上是一个不能更改的列表。 There are other immutable versions of common data types such as frozensets instead of sets. 普通数据类型还有其他不可变版本,例如冻结集而不是集。

Change those lists to tuples and you'll be able to hash them! 将这些列表更改为元组,即可对它们进行哈希处理!

Try this code. 试试这个代码。 The main idea is convert the [v]+[i] values in dict1 and dict2 to tuple, then calculate the difference of dict1 and dict2. 主要思想是将dict1和dict2中的[v]+[i]值转换为元组,然后计算dict1和dict2之差。 Finally, convert the tuple type values back to list. 最后,将元组类型的值转换回list。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM