类型错误 Unhashable type:set [英] Type error Unhashable type:set
问题描述
下面的代码在函数 U=set(p.enum()) 中有一个错误,这是一个不可散列类型的类型错误:'set' 实际上,如果你能看到类方法 enum 返回 'L',它是集合和函数中的 U 应该是一个集合,所以你能帮我解决这个问题,或者我如何将集合列表转换为集合集合?
The below code has an error in function U=set(p.enum()) which a type error of unhashable type : 'set' actually if you can see the class method enum am returning 'L' which is list of sets and the U in function should be a set so can you please help me to resolve the issue or How can I convert list of sets to set of sets?
class pattern(object):
def __init__(self,node,sets,cnt):
self.node=node
self.sets=sets
self.cnt=cnt
def enum(self):
L=[]
if self.cnt==1:
L = self.node
else:
for i in self.sets:
L=[]
for j in self.node:
if i!=j:
L.append(set([i])|set([j]))
return L #List of sets
V=set([1,2,3,4])
U=set()
cnt=1
for j in V:
p=pattern(V,(U|set([j])),cnt)
U=set(p.enum()) #type error Unhashable type:'set'
print U
cnt+=1
推荐答案
您放入集合中的单个项目不能是可变的,因为如果它们更改,有效散列值将更改,从而能够检查是否包含会崩溃.
The individual items that you put into a set can't be mutable, because if they changed, the effective hash would change and thus the ability to check for inclusion would break down.
相反,您需要将不可变对象放入集合中 - 例如frozenset
s.
Instead, you need to put immutable objects into a set - e.g. frozenset
s.
如果您将返回语句从 enum
方法更改为...
If you change the return statement from your enum
method to...
return [frozenset(i) for i in L]
...那么它应该可以工作.
...then it should work.
这篇关于类型错误 Unhashable type:set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!