英文:
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.]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论