如何基于搜索返回numpy ndarray的索引? [英] How to return the index of numpy ndarray based on search?

查看:57
本文介绍了如何基于搜索返回numpy ndarray的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy的2D数组

I have a numpy 2D array,

import numpy as np
array1 = array([[ 1,  2,  1,  1],
   [ 2,  2,  2,  1],
   [ 1,  1,  1,  1],
   [1,  3, 1, 1],
   [1,  1, 1, 1]])

我想找到元素"3"并知道其位置.所以,我可以尝试

I would like to find the element '3' and know its location. So, I could try

condition = array1 == 3

然后找到参数,我可以尝试

and then to find the arguments, I could try

np.argwhere(condition)

一个人如何将索引定位到3所在的位置?

How else does one locate the indices where 3 is located?

推荐答案

您还可以使用 where ,返回一个坐标元组:

You can also use where, returning a tuple of coordinate:

In [34]: np.where(array1==3)
Out[34]: (array([3]), array([1]))

这篇关于如何基于搜索返回numpy ndarray的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆