Python Numpy:查找范围内元素的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13869173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:47:41  来源:igfitidea点击:

Numpy: find index of the elements within range

pythonnumpy

提问by Bob

I have a numpy array of numbers, for example,

例如,我有一个 numpy 数字数组,

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

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?

我想找到特定范围内元素的所有索引。例如,如果范围是 (6, 10),那么答案应该是 (3, 4, 5)。是否有内置函数来执行此操作?

采纳答案by deinonychusaur

You can use np.whereto get indices and np.logical_andto set two conditions:

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

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]),)

回答by Bi Rico

I thought I would add this because the ain the example you gave is sorted:

我想我会添加这个,因为a在你给出的例子中是排序的:

import numpy as np
a = [1, 3, 5, 6, 9, 10, 14, 15, 56] 
start = np.searchsorted(a, 6, 'left')
end = np.searchsorted(a, 10, 'right')
rng = np.arange(start, end)
rng
# array([3, 4, 5])

回答by tiago

As in @deinonychusaur's reply, but even more compact:

就像@deinonychusaur 的回复一样,但更紧凑:

In [7]: np.where((a >= 6) & (a <=10))
Out[7]: (array([3, 4, 5]),)

回答by Vigneshwaran Narayanan

s=[52, 33, 70, 39, 57, 59, 7, 2, 46, 69, 11, 74, 58, 60, 63, 43, 75, 92, 65, 19, 1, 79, 22, 38, 26, 3, 66, 88, 9, 15, 28, 44, 67, 87, 21, 49, 85, 32, 89, 77, 47, 93, 35, 12, 73, 76, 50, 45, 5, 29, 97, 94, 95, 56, 48, 71, 54, 55, 51, 23, 84, 80, 62, 30, 13, 34]

dic={}

for i in range(0,len(s),10):
    dic[i,i+10]=list(filter(lambda x:((x>=i)&(x<i+10)),s))
print(dic)

for keys,values in dic.items():
    print(keys)
    print(values)

Output:

输出:

(0, 10)
[7, 2, 1, 3, 9, 5]
(20, 30)
[22, 26, 28, 21, 29, 23]
(30, 40)
[33, 39, 38, 32, 35, 30, 34]
(10, 20)
[11, 19, 15, 12, 13]
(40, 50)
[46, 43, 44, 49, 47, 45, 48]
(60, 70)
[69, 60, 63, 65, 66, 67, 62]
(50, 60)
[52, 57, 59, 58, 50, 56, 54, 55, 51]  

回答by Abhishek

a = np.array([1,2,3,4,5,6,7,8,9])
b = a[(a>2) & (a<8)]

回答by Deba Pratim Saha

You can use np.clip()to achieve the same:

您可以使用以下np.clip()方法实现相同的目的:

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

However, it holds the values less than and greater than 6 and 10 respectively.

但是,它分别保存小于和大于 6 和 10 的值。

回答by Abhishek

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

回答by Nathan

This code snippet returns all the numbers in a numpy array between two values:

此代码段返回两个值之间的 numpy 数组中的所有数字:

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

It works as following: (a>6) returns a numpy array with True (1) and False (0), so does (a<10). By multiplying these two together you get an array with either a True, if both statements are True (because 1x1 = 1) or False (because 0x0 = 0 and 1x0 = 0).

它的工作原理如下:(a>6) 返回一个包含 True (1) 和 False (0) 的 numpy 数组,(a<10) 也是如此。通过将这两者相乘,您将得到一个带有 True 的数组,如果两个语句都是 True(因为 1x1 = 1)或 False(因为 0x0 = 0 和 1x0 = 0)。

The part a[...] returns all values of array a where the array between brackets returns a True statement.

a[...] 部分返回数组 a 的所有值,其中括号之间的数组返回 True 语句。

Of course you can make this more complicated by saying for instance

当然,您可以通过说例如使这更复杂

...*(1-a<10) 

which is similar to an "and Not" statement.

这类似于“and Not”语句。

回答by cvanelteren

This may not be the prettiest, but works for any dimension

这可能不是最漂亮的,但适用于任何维度

a = np.array([[-1,2], [1,5], [6,7], [5,2], [3,4], [0, 0], [-1,-1]])
ranges = (0,4), (0,4) 

def conditionRange(X : np.ndarray, ranges : list) -> np.ndarray:
    idx = set()
    for column, r in enumerate(ranges):
        tmp = np.where(np.logical_and(X[:, column] >= r[0], X[:, column] <= r[1]))[0]
        if idx:
            idx = idx & set(tmp)
        else:
            idx = set(tmp)
    idx = np.array(list(idx))
    return X[idx, :]

b = conditionRange(a, ranges)
print(b)

回答by Jorge Cruz

Other way is with:

另一种方法是:

np.vectorize(lambda x: 6 <= x <= 10)(a)

which returns:

返回:

array([False, False, False,  True,  True,  True, False, False, False])

It is sometimes useful for masking time series, vectors, etc.

它有时可用于屏蔽时间序列、向量等。