Coordinates of item on NumPy array

后端 未结 2 1373
暖寄归人
暖寄归人 2021-02-05 09:18

I have a NumPy array:

[[  0.   1.   2.   3.   4.]
 [  7.   8.   9.  10.   4.]
 [ 14.  15.  16.  17.   4.]
 [  1.  20.  21.  22.  23.]
 [ 27.  28.   1.  20.  29.]         


        
2条回答
  •  死守一世寂寞
    2021-02-05 10:04

    If a is your array, then you could use:

    ii = np.nonzero(a == 4)
    

    or

    ii = np.where(a == 4)
    

    If you really want a tuple, you can convert from the tuple of arrays to the tuple of tuples, but the return value from the numpy functions is convient for then doing other operations on your array.

    Conversion to a tuple for the OP's specification:

    tuple(zip(*ii))
    

提交回复
热议问题