使用 zip() 和 np.delete() 从相同位置的 numpy 数组中删除元素



我得到了两个相同长度的一维数组(itl),我想删除ly的元素以进行l<ls,并删除it中相同位置的元素x。我试过了:>

for x, y in zip(it, l):
if y < ls:
np.delete(l,y)
np.delete(it,x)

但它不起作用,并且有警告:

"弃用警告:在删除中使用非整数数组作为 obj 将导致将来出错" NP.delete(L,Y)

"弃用警告:在删除中使用非整数数组作为 obj 将导致将来出错 np.delete(it,x)">

这里有一个例子。

您必须指示要删除的元素的索引:

import numpy as np
it = np.asarray(range(10))
l = np.asarray(range(10))
ls = 4
index = []
for i, y in enumerate(l):
if y < ls:
index.append(i)
l = np.delete(l, index)
it = np.delete(it, index)

结果:

(array([4, 5, 6, 7, 8, 9]), array([4, 5, 6, 7, 8, 9]))

较短的版本 :

index = np.argwhere(l < ls)
l = np.delete(l, index)
it = np.delete(it, index)

最新更新