首页 > 解决方案 > 在numpy数组中过滤

问题描述

想要numpy array基于仅使用 numpy 的条件进行过滤

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

Op:

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

Expected OP:

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

指出我的错误会很棒

标签: python-3.xnumpynumpy-ndarray

解决方案


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

(数组([0, 1]),)

对应于真实元素的索引。

您的预期输出由下式给出:

np.array(["p" in s for s in sample]).astype(int)

推荐阅读