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

前端 未结 4 972
死守一世寂寞
死守一世寂寞 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)))
    

提交回复
热议问题