>>> import numpy
>>> x = numpy.array([1, 0, 2, 0, 3, 0, 4, 5, 6, 7, 8])
>>> numpy.where(x == 0)[0]
array([1, 3, 5])
>>> numpy.where(x == 0)
Out[14]: array([1, 3, 5], dtype=int64)
import numpy as np
a = np.asarray([0, 1, 2, 3, 4])
a == 0 # or whatever
Out[15]: array([ True, False, False, False, False])
import numpy as np
arr = np.array([[1, 2, 3], [0, 1, 0], [7, 0, 2]])
arr
Out[18]:
array([[1, 2, 3],
[0, 1, 0],
[7, 0, 2]])
它将所有找到的索引作为行返回:
np.argwhere(arr == 0)
Out[16]:
array([[1, 0], # Indices of the first zero
[1, 2], # Indices of the second zero
[2, 1]], # Indices of the third zero
dtype=int64)
import numpy as np
x = np.array([1, 0, 2, 0, 3, 0, 4, 5, 6, 7, 8])
x == 0
Out[20]:
array([False, True, False, True, False, True, False, False, False,
False, False])
np.nonzero(x == 0)[0]
Out[21]: array([1, 3, 5], dtype=int64)
import numpy as np
x = np.array([1, 0, 2, 0, 3, 0, 4, 5, 6, 7, 8])
np.flatnonzero(x == 0)
Out[24]: array([1, 3, 5], dtype=int64)
import numpy as np
x = np.array([1, 0, 2, 3, 6])
non_zero_arr = np.extract(x > 0, x)
non_zero_arr
Out[28]: array([1, 2, 3, 6])
min_index = np.amin(non_zero_arr)
min_index
Out[26]: 1
min_value = np.argmin(non_zero_arr)
min_value
Out[27]: 0