问题
方法
import numpy as np a = np.random.randint(-1, 2, (10,)) print(a) # [ 1 -1 0 0 0 -1 1 1 -1 0] b = np.flatnonzero(a == -1) # 返回a所有等于-1元素索引 print(b) # [1 5 8] c = np.flatnonzero(a) # 返回a所有非零元素索引 print(c) # [0 1 5 6 7 8] ''' [[ 1 1 -1] [-1 0 1] [ 0 1 -1]] ''' d = np.random.randint(-1, 2, (3,3)) e = np.flatnonzero(d) print(e) # [0 1 2 3 5 7 8]