I have a numpy array of letters 'x' and 'y'. How do I make a boolean array for this that returns true if index == 'x' and false otherwise?
2 Answers
A more general approach, useful for multiple letter conditions:
base_mask = lambda x: x == 'A' or x == 'a' or x == 'T' or x == 't'
vec_base_mask = np.vectorize(base_mask)
seq = 'TCTATcgGGa'
a = np.array([*seq])
masked_a = vec_base_mask(a)
# >>> masked_a
# array([ True, False, True, True, True, False, False, False, False,
# True])
numpy.charfor vectorized versions of things likeupper,isspace, etc: docs.scipy.org/doc/numpy/reference/routines.char.html