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

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

        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)
    

提交回复
热议问题