Unable to convert list into set, raises “unhashable type: 'list' ” error

前端 未结 2 956
深忆病人
深忆病人 2021-01-13 16:44

So I\'m trying to find all sub-lists of a list and here is what I have now. I\'m new to Python and I don\'t understand why \" Q3_ans=set(ans)\" raises an error. I\'ve tried

相关标签:
2条回答
  • 2021-01-13 17:25

    Here's the essence of the problem:

    >>> set([[["a"],["b"],["a","b"]]])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
    

    So, what does that mean?

    set([iterable])

    Return a new set ... object whose elements are taken from iterable. The elements of a set must be hashable.

    hashable

    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__() 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; mutable containers (such as lists or dictionaries) are not.

    The keywords here are mutable

    Mutable objects can change their value but keep their id().

    and immutable

    An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.

    So, you can't use a list as set element. A tuple would work:

    >>> set([(("a"),("b"),("a","b"))])
    {('a', 'b', ('a', 'b'))}
    
    0 讨论(0)
  • 2021-01-13 17:28

    As you can figure out why, mutable types like lists can't be hashable, so can't be converted to a set. You can try returning tuple instead; an immutable counterpart for list:

    def f2(seq):
        assert len(seq)==2
        assert isinstance(x, tuple) # what's `x` actually?
        a, b = seq
        return ((a), (b), (a,b))
    
    def all_sublists(x):
        assert isinstance(x, list)
        ans = []
        for i in range(0, len(x) - 1):
            for j in range(1, len(x)):
                temp = (x[i], x[j])
                temp = [f2(temp)]
                ans.extend(temp)
        Q3_ans = set(tuple(ans))
        return Q3_ans
    

    then

    all_sublists([1, 2, 3])
    

    You can read more about tuple type in the documentation.

    0 讨论(0)
提交回复
热议问题