Iterate over numpy with index (numpy equivalent of python enumerate)

后端 未结 3 1157
甜味超标
甜味超标 2020-12-20 19:11

I\'m trying to create a function that will calculate the lattice distance (number of horizontal and vertical steps) between elements in a multi-dimensional numpy array. For

3条回答
  •  旧时难觅i
    2020-12-20 20:02

    Another possible solution:

    import numpy as np
    
    A=np.array([[1,2,3],[4,5,6],[7,8,9]])
    for _, val in np.ndenumerate(A):
        ind = np.argwhere(A==val)
        print val, ind
    

    In this case you will obtain the array of indexes if value appears in array not once.

提交回复
热议问题