numpy:找到范围内元素的索引 [英] Numpy: find index of the elements within range

查看:468
本文介绍了numpy:找到范围内元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字数组,例如

I have a numpy array of numbers, for example,

a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])  

我想找到特定范围内元素的所有索引.例如,如果范围是(6,10),则答案应该是(3,4,5).有内置的功能可以做到这一点吗?

I would like to find all the indexes of the elements within a specific range. For instance, if the range is (6, 10), the answer should be (3, 4, 5). Is there a built-in function to do this?

推荐答案

您可以使用np.where获取索引,并使用np.logical_and设置两个条件:

You can use np.where to get indices and np.logical_and to set two conditions:

import numpy as np
a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])

np.where(np.logical_and(a>=6, a<=10))
# returns (array([3, 4, 5]),)

这篇关于numpy:找到范围内元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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