Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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  
like image 482
user2014111 Avatar asked May 10 '14 06:05

user2014111


People also ask

How do I fix Unhashable type error?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

What does Unhashable type set mean Python?

The Python "TypeError: unhashable type: 'set'" occurs when we use a set as a key in a dictionary or an element in another set . To solve the error, use a frozenset instead, because set objects are mutable and unhashable.

How do you fix a TypeError Unhashable type slice?

Slice is not a hashable object, and therefore it cannot be used as a key to a dictionary. To solve this error, specify the appropriate key names for the values you want or use an iterable object like items() and iterate over the items in the dictionary.

How do I fix Unhashable type NumPy Ndarray?

We can solve this by adding each array element instead of the array object into the set. This should add all the elements of the array to the set.


2 Answers

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.

like image 67
Amber Avatar answered Sep 16 '22 13:09

Amber


This error is raised because a set can only contain immutable types. Or sets are mutable. However there is the frozenset type :

In [4]: a, b = {1,2,3}, {2,3,4}  In [5]: set([a,b]) --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-5-6ca6d80d679c> in <module>() ----> 1 set([a,b])  TypeError: unhashable type: 'set'  In [6]: a, b = frozenset({1,2,3}), frozenset({2,3,4})  In [7]: set([a,b]) Out[7]: {frozenset({1, 2, 3}), frozenset({2, 3, 4})} 
like image 32
Kiwi Avatar answered Sep 18 '22 13:09

Kiwi