Andy
Andy

Reputation: 53

How to return index location where np.allclose() is True

I have a Python question, I hope someone could help me with this. I have a list of 500+ indices of numpy array. I want to compare that list of indices to another set of numpy array and return the index location where the value of the comparison is True. I've looked up the np.allclose with my own tolerance to return the value of True whether two sets of arrays are similar enough. But i don't know how to return the location of that index. I've found the list.index() function online but don't know how to implement the code for my situation

For example, I have 2 lists A and B. A has 500+ indices of numpy array in it. And B an array. I can use the np.allclose() function to return a boolean when I compare B with A. But I also want to know where in the list of A, np.allclose() returns True.

I hope the example is clear enough :D

If anyone could help, it would be great. Thanks

Upvotes: 2

Views: 1792

Answers (1)

hpaulj
hpaulj

Reputation: 231540

np.nonzero (also called np.where) gives indices of True elements of an array (or list:

In [4]: np.nonzero([False, False, True, False, False])
Out[4]: (array([2]),)

There are lots of SO questions about using this form of np.where.

Upvotes: 3

Related Questions