python - 从一个数组中删除另一个数组中的所有数字

标签 python arrays numpy

我有一个数组“removable”,其中包含另一个数组“All”中的一些数字,该数组包含从 0 到 k 的所有数字。

我想删除列在 removable 中的 A 中的所有数字。

All = np.arange(k)
removable = np.ndarray([1, 3, 4 , 7, 9, ..., 200])

for i in removable:
    if i in All:
        All.remove(i)

ndarray 没有 remove 属性,但我确信 numpy 中有一个简单的方法可以解决这个问题,但我在文档中找不到它。

最佳答案

您可以使用函数 setdiff1d来自 NumPy:

>>> a = np.array([1, 2, 3, 2, 4, 1])
>>> b = np.array([3, 4, 5, 6])
>>> np.setdiff1d(a, b)
array([1, 2])

关于python - 从一个数组中删除另一个数组中的所有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54536979/

相关文章:

ios - Swift : value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?' ? 问题

Ruby 创建数组形式的 git 日志?

python - 升级 numpy 失败,出现 "Permission denied"错误

python - 需要 Numpy 帮助 : how to use boolean values to calculate ranges and add values together within ranges?

python - 在数据立方体中使用列表理解

python - 在 Pydev/Eclipse 的 python 程序中使用外部 C 库

python - 有条件地从列表中选择下一个元素

python - wxPython 无法与 Python 3.7 一起使用(在 Windows 7,64 位上)?

python - 意外 'reflowed' ,如何撤消长换行符?

javascript - 如何正确使用 setTimeout 和立即调用的函数?