python - 如何在不知道索引的情况下从 numpy 数组中删除对象

标签 python list numpy

是否可以在不知道对象索引但知道对象本身的情况下从 numpy 数组中删除对象?

我已经看到可以使用 np.delete 函数使用对象的索引,但我正在寻找一种方法来使用对象而不是它的索引。

例子:

[a,b,c,d,e,f]

x = e

我想删除 x

最佳答案

您可以使用 np.argwhere 找到对象的索引/索引,然后使用 np.delete 删除对象.

例子:

x = np.array([1,2,3,4,5])
index = np.argwhere(x==3)
y = np.delete(x, index)
print(x, y)

关于python - 如何在不知道索引的情况下从 numpy 数组中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36365990/

相关文章:

html - Firefox - 宽度 : 100% not working for list in a list (display: table;)

python - 具有 3D 向量的两个 numpy 数组的点积

python - 如何使用一个 numpy bool 数组修改另一个 numpy 数组?

python - Pymongo 根据第一个集合中链接的 ObjectId 查询第二个集合

python - 将 Python ctypes.Structure 转换为 str

从模块导入函数而从另一个模块导入类时出现 Python 导入错误 (ModuleNotFoundError)

python - 选择我希望列中的值具有多个值的行

list - 合并 2 个元组列表并在元组中添加非唯一值的 Pythonic 方法是什么?

python - 单个元素的访问比列表慢

python-3.x - Python - 从 URL 读取图像以调整图像大小并将图像转换为灰度