Constructing a Python set from a Numpy matrix

前端 未结 6 1075
面向向阳花
面向向阳花 2020-12-06 00:17

I\'m trying to execute the following

>> from numpy import *
>> x = array([[3,2,3],[4,4,4]])
>> y = set(x)
TypeError: unhashable type: \'num         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 00:59

    The immutable counterpart to an array is the tuple, hence, try convert the array of arrays into an array of tuples:

    >> from numpy import *
    >> x = array([[3,2,3],[4,4,4]])
    
    >> x_hashable = map(tuple, x)
    
    >> y = set(x_hashable)
    set([(3, 2, 3), (4, 4, 4)])
    

提交回复
热议问题