Python-numpy.searchsorted()

0.函数定义

searchsorted(a, v, side='left', sorter=None)

Find indices where elements should be inserted to maintain order.

Find the indices into a sorted array `a` such that, if the corresponding elements in `v` were inserted before the indices, the order of `a` would be preserved.

1.参数详解

a : 1-D array_like

输入数组。当sorter参数为None的时候,a必须为升序数组;否则,sorter不能为空,存放a中元素的index,用于反映a数组的升序排列方式。
 

v : array_like
插入a数组的值,可以为单个元素,list或者array。

side : {'left', 'right'}, optional

查询方向:

扫描二维码关注公众号,回复: 4877159 查看本文章

当为left时,将返回第一个符合条件的元素下标;

当为right时,将返回最后一个符合条件的元素下标,如果没有符合的元素,将返回0或者N(a的长度)

side returned index i satisfies
left a[i-1] < v <= a[i]
right a[i-1] <= v < a[i]

sorter : 1-D array_like, optional

存放a数组元素的index,index对应元素为升序。
 

2.示例

import numpy as np

a = np.array([0,1,5,9,11,18,26,33])
print(a)
result = np.searchsorted(a,15,side='left',sorter=None)
print(result)

b = a
np.random.shuffle(b)
b_sort = np.argsort(b)
print(b)
print(b_sort)
result2 = np.searchsorted(a,[15.0,2],side='left',sorter=b_sort)
print(result2)

猜你喜欢

转载自blog.csdn.net/qq_17753903/article/details/85165637