TypeError : Unhashable type

前端 未结 6 684
灰色年华
灰色年华 2020-11-29 05:45

I am trying to get a list of list of tuples : something like [ [(1,0),(2,0),(3,0)],[(1,1),(2,1),(3,1)....]] I used this statement

set([(a,b)for         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-29 06:16

    TLDR:

    - You can't hash a list, a set, nor a dict to put that into sets

    - You can hash a tuple to put it into a set.

    Example:

    >>> {1, 2, [3, 4]}
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    >>> {1, 2, (3, 4)}
    set([1, 2, (3, 4)])
    

    Note that hashing is somehow recursive and the above holds true for nested items:

    >>> {1, 2, 3, (4, [2, 3])}
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    

    Dict keys also are hashable, so the above holds for dict keys too.

提交回复
热议问题