numpy.flatnonzero(a)
- a:输入array
- 返回:一个数组,该数组是a的非0索引
该函数输入一个矩阵,返回array的非0索引(也可以指定索引元素,下面会展示)
Return indices that are non-zero in the flattened version of a.
import numpy as np
x = np.arange(-2 ,3)
print(x)
y = np.flatnonzero(x)
print(y)
y_ = x[np.flatnonzero(x)]
print(y_)
'''
[-2 -1 0 1 2]
[0 1 3 4]
[-2 -1 1 2]
'''
# also can use like this
xx = np.array([1, 3, 2, 3, 4, 3, 1, 2])
yy = np.flatnonzero(xx == 3)
print(yy)
'''
[1 3 5]
'''