Home > Blockchain >  The best way to find index of point in nd array in python
The best way to find index of point in nd array in python

Time:04-26

I am using Python3 and numpy. I have an nd array like:

arr = np.array([[1,2], [3,4],[5,6]])

I want to find position of point for example [3,4]. I am using this

idP = [id for id in range(len(arr)) if (arr[id] == [3,4]).all()]

what is the best way to do this

CodePudding user response:

You can do it by:

(arr == [3, 4]).all(-1).nonzero()[0]

or by using np.isin:

np.isin(arr, [3, 4]).all(1).nonzero()[0]

CodePudding user response:

One solution could be converting you numpy array into a python list

arr.tolist().index([3,4])
>>> 1

CodePudding user response:

How about this?

arr.tolist().index([3,4])
  • Related