TypeError: unhashable type: 'list' when using built-in set function

前端 未结 4 950
死守一世寂寞
死守一世寂寞 2020-11-27 14:57

I have a list containing multiple lists as its elements

eg: [[1,2,3,4],[4,5,6,7]]

If I use the built in set function to remove duplicates f

相关标签:
4条回答
  • 2020-11-27 15:21

    Sets require their items to be hashable. Out of types predefined by Python only the immutable ones, such as strings, numbers, and tuples, are hashable. Mutable types, such as lists and dicts, are not hashable because a change of their contents would change the hash and break the lookup code.

    Since you're sorting the list anyway, just place the duplicate removal after the list is already sorted. This is easy to implement, doesn't increase algorithmic complexity of the operation, and doesn't require changing sublists to tuples:

    def uniq(lst):
        last = object()
        for item in lst:
            if item == last:
                continue
            yield item
            last = item
    
    def sort_and_deduplicate(l):
        return list(uniq(sorted(l, reverse=True)))
    
    0 讨论(0)
  • 2020-11-27 15:21
        python 3.2
    
    
        >>>> from itertools import chain
        >>>> eg=sorted(list(set(list(chain(*eg)))), reverse=True)
            [7, 6, 5, 4, 3, 2, 1]
    
    
       ##### eg contain 2 list within a list. so if you want to use set() function
       you should flatten the list like [1, 2, 3, 4, 4, 5, 6, 7]
    
       >>> res= list(chain(*eg))       # [1, 2, 3, 4, 4, 5, 6, 7]                   
       >>> res1= set(res)                    #   [1, 2, 3, 4, 5, 6, 7]
       >>> res1= sorted(res1,reverse=True)
    
    0 讨论(0)
  • 2020-11-27 15:31

    Sets remove duplicate items. In order to do that, the item can't change while in the set. Lists can change after being created, and are termed 'mutable'. You cannot put mutable things in a set.

    Lists have an unmutable equivalent, called a 'tuple'. This is how you would write a piece of code that took a list of lists, removed duplicate lists, then sorted it in reverse.

    result = sorted(set(map(tuple, my_list)), reverse=True)

    Additional note: If a tuple contains a list, the tuple is still considered mutable.

    Some examples:

    >>> hash( tuple() )
    3527539
    >>> hash( dict() )
    
    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in <module>
        hash( dict() )
    TypeError: unhashable type: 'dict'
    >>> hash( list() )
    
    Traceback (most recent call last):
      File "<pyshell#6>", line 1, in <module>
        hash( list() )
    TypeError: unhashable type: 'list'
    
    0 讨论(0)
  • 2020-11-27 15:44

    Definitely not the ideal solution, but it's easier for me to understand if I convert the list into tuples and then sort it.

    mylist = [[1,2,3,4],[4,5,6,7]]
    mylist2 = []
    for thing in mylist:
        thing = tuple(thing)
        mylist2.append(thing)
    set(mylist2)
    
    0 讨论(0)
提交回复
热议问题