np.flatnonzero
numpy
本文字数:129 字 | 阅读时长 ≈ 1 min

np.flatnonzero

numpy
本文字数:129 字 | 阅读时长 ≈ 1 min

numpy.flatnonzero(a)

该函数输入一个矩阵,返回 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]
'''
9月 09, 2024