是否有一个numpy的函数返回的东西的第一个索引在数组中? [英] Is there a Numpy function to return the first index of something in an array?

查看:282
本文介绍了是否有一个numpy的函数返回的东西的第一个索引在数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有Python的列表,返回的东西第一个索引的方法

I know there is a method for python list to return the first index of something

l = list(1,2,3)
l.index(2)
>>>  1

有没有类似的东西为numpy的阵列?

Is there something like that for numpy arrays?

推荐答案

是的,这里是给予numpy的数组,数组的答案,和值,物品,以进行搜索。

Yes, here is the answer given a Numpy array, array, and a value, item, to search for.

itemindex = numpy.where(array==item)

的结果是一个元组与第一所有的行指数,那么所有的列索引

The result is a tuple with first all the row indices, then all the column indices.

例如,如果数组为两个维度,它在两个位置包含在您的项目,然后

For example if array is two dimensions and it contained your item at two locations then

array[itemindex[0][0]][itemindex[1][0]]

将等于您的项目,因此会

would be equal to your item and so would

array[itemindex[0][1]][itemindex[1][1]]

numpy.where

这篇关于是否有一个numpy的函数返回的东西的第一个索引在数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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