Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

python - Type error Unhashable type:set

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Instead, you need to put immutable objects into a set - e.g. frozensets.

If you change the return statement from your enum method to...

return [frozenset(i) for i in L]

...then it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...