2

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?

1

2 Answers 2

1
In [1]: import numpy as np

In [2]: a = np.array(['x', 'y', 'y', 'x', 'y'])

In [3]: a == 'x'
Out[3]: array([ True, False, False,  True, False], dtype=bool)
Sign up to request clarification or add additional context in comments.

Comments

0

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.