Python-numpy.searchsorted()
阿新 • • 發佈:2019-01-11
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
查詢方向:
當為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)