python-3.x - 在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),)
指出我的错误会很棒
解决方案
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)
推荐阅读
- amazon-web-services - 用于创建触发 lambda 函数的云监视事件的 Terraform 代码
- java - 没有可用的名为“DAOBean”的 bean
- javascript - Jquery cookie插件从多个项目中删除项目
- networking - 如何知道我的测试(使用 JMeter)将产生的网络流量?
- java - 返回总和最大的数组。如果出现平局,则返回
- python - 我如何制作值元组?
- c - CMake:在工具链文件中添加编译器标志
- python - 使用 Dash 和 Plotly 的多页仪表板
- .net - 使用“DirectoryNotEmptyException”删除 .sonartmp 文件的 Sonarqube 扫描仪错误
- schema - 如何定义 Avro Schema 来捕获可能会出现或不会出现的键?