有一个NumPy函数可以根据条件从两个数组中删除元素吗?

huangapple go评论129阅读模式
英文:

is there a numpy function to delete elements in two arrays given a condition?

问题

我想删除两个数组中等于0.0的元素,以及在一个数组中删除0.0元素后,在另一个数组中相同位置的非零元素。我期望的结果如下:

a = np.array([5.0, 10.0])
b = np.array([1.0, 1.0])
英文:

I have two arrays:

a = np.array([0.0, 3.0, 5.0, 0.0, 10.0])
b = np.array([0.0, 0.0, 1.0, 4.0, 1.0 ])

I want to delete elements equal to 0.0 from both lists and the non-zero elements which are in the same position of a deleted 0.0 element in the other list. Exactly, what I expect is:

a = np.array([5.0, 10.0])
b = np.array([1.0, 1.0 ])

thanks!

答案1

得分: 2

尝试

```py
a = np.array([0.0, 3.0, 5.0, 0.0, 10.0])
b = np.array([0.0, 0.0, 1.0, 4.0, 1.0])

mask = ~((a == 0) | (b == 0))

a = a[mask]
b = b[mask]

print(a)
print(b)

输出:

[ 5. 10.]
[1. 1.]

<details>
<summary>英文:</summary>

Try:

```py
a = np.array([0.0, 3.0, 5.0, 0.0, 10.0])
b = np.array([0.0, 0.0, 1.0, 4.0, 1.0 ])

mask = ~((a == 0) | (b == 0))

a = a[mask]
b = b[mask]

print(a)
print(b)

Prints:

[ 5. 10.]
[1. 1.]

huangapple
  • 本文由 发表于 2023年4月7日 04:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定