0

Want to filter numpy array based on a condition with using only numpy

sample = ["aple","mangp", "orange"]
np.where("p" in sample)

Op:

(array([], dtype=int64),)

Expected OP:

(array([1,1,0], dtype=int64),)

Pointers on my mistake would be great

Swavig
  • 35
  • 5

1 Answers1

0
sample = ["aple", "mangp", "orange"]
np.where(["p" in s for s in sample])

(array([0, 1]),)

which corresponds to the indexes of the true elements.

Your expected output is just given by:

np.array(["p" in s for s in sample]).astype(int)
dzang
  • 2,160
  • 2
  • 12
  • 21