Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组中每个非零元素的返回索引_Python_Arrays_Numpy - Fatal编程技术网

Python 数组中每个非零元素的返回索引

Python 数组中每个非零元素的返回索引,python,arrays,numpy,Python,Arrays,Numpy,我知道有一种方法可以返回python中数组中最大元素的索引:numpy.argmax()。是否有方法返回每个非零元素的索引 比如说 array([[ 0., 1., 1., ..., 1., 0., 0.], [ 0., 1., 1., ..., 1., 0., 1.], [ 0., 1., 1., ..., 1., 0., 0.], ..., [ 0., 1., 1., ..., 1., 0.,

我知道有一种方法可以返回python中数组中最大元素的索引:
numpy.argmax()
。是否有方法返回每个非零元素的索引

比如说

array([[ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  1.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       ..., 
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.]], dtype=float32)


你想要这样的东西吗:

x = np.asarray([0, 1, 2, 3, 0, 1])
In [129]: np.nonzero(x)
Out[129]: (array([1, 2, 3, 5]),)

请参阅:,

您想要这样的东西吗:

x = np.asarray([0, 1, 2, 3, 0, 1])
In [129]: np.nonzero(x)
Out[129]: (array([1, 2, 3, 5]),)

请参阅:,

是。我无法相信为什么我没有从谷歌得到正确的答案。是的。我无法相信为什么我没有从谷歌得到正确的答案。