Hashing arrays in Python

前端 未结 3 1716
难免孤独
难免孤独 2020-12-05 09:08

Is it possible to hash lists?

For example, I know that hashes of tuples are possible:

>>> hash((1,2,3,4,5,6))
-319527650
         


        
3条回答
  •  情话喂你
    2020-12-05 09:46

    Just try it:

    >>> hash((1,2,3))
    2528502973977326415
    >>> hash([1,2,3])
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    >>> hash(frozenset((1,2,3)))
    -7699079583225461316
    >>> hash(set((1,2,3)))
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'set'
    

    So you can get hash of tuple and frozenset since the are immutable, and you can't do it for list and set because they are mutable.

提交回复
热议问题