numpy.any
will return True
/False
, so that won't be suitable to use here.
You can stick to just numpy.where
, but with a small tweak in the condition -- you introduce the bitwise OR operator into the condition like this:
np.where((letters == 'A') | (letters == 'C'))
In fact, numpy
has this built in as well, as numpy.bitwise_or
:
np.where(np.bitwise_or(letters == 'A', letters == 'C'))
Here's a short working example:
import numpy as np
A = 'A'
B = 'B'
C = 'C'
letters = np.array([A, B, C, A, B, C, A, B, C])
# Either this:
letter_indexes = np.where((letters == 'A') | (letters == 'C'))
# Or this:
letter_indexes = np.where(np.bitwise_or(letters == 'A', letters == 'C'))
print(letter_indexes[0])
# [0 2 3 5 6 8]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…