Roma  Karageorgievich Roma Karageorgievich - 1 year ago 238
Python Question

Find indexes by numpy where

I have numpy array list:

x = ["A", "A", "B", "A", "C", "D", "B"]

For finding indexes of
I use
np.where(x == "A")
and it works fine.
How can I find indexes of

I used:

np.where(x == ["A", "B"])
np.where(x in ["A", "B"])
np.where(x == any(["A", "B"]))

But it didn't help me.

Answer Source

Use | or np_logical_or for element-wise OR:

x = np.array(x)
np.where((x=='A') | (x=='B'))
Out: (array([0, 1, 2, 3, 6], dtype=int64),)

np.where(np.logical_or(x=='A', x=='B'))
Out: (array([0, 1, 2, 3, 6], dtype=int64),)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download