Roma Karageorgievich - 3 months ago 19

Python Question

I have numpy array list:

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

For finding indexes of

`"A"`

`np.where(x == "A")`

How can I find indexes of

`"A"`

`"B"`

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

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),)
```

Source (Stackoverflow)

Comments