guide
We often use numpy in python data manipulation, it is very convenient and fast to operate arrays through numpy
Element screening
- One-dimensional array
Filter out the array equal to a certain value in the array
import numpy as np
a = np.array([1,2,3,4,2,4,5])
#筛选出元素
print(a[a == 2])
#筛选出元素的下标
print(np.where(a == 2))
- Multidimensional array
The target element of filtering is a multidimensional array, at this time we need to useall
andany
function
import numpy as np
a = np.array([[0,0],[1,1],[2,2],[0,0],[3,4],[1,5]])
#目标元素
target = np.array([0,0])
#筛选出元素
print(a[(a==target).all(axis=1)])
#目标元素
target = np.array([1,2])
#筛选出第一列包含1或者第二列包含2的元素
print(a[(a==target).any(axis=1)])