python中的搜索排序是如何工作的? [英] how does searchsort in python work?
问题描述
为了清楚我的问题说如果我有一个数组 a 作为出[123]:[1, 3, 4, 6, 9, 10, 54]当我尝试搜索列表中的数字时,searchsort 返回正确的值,但是当我尝试不在列表中的内容时,它返回一个荒谬的值
To make my question clear say if I have an array a as Out[123]: [1, 3, 4, 6, 9, 10, 54] When I try to search the numbers in the list, searchsort returns correct value but when I try something not in the list, it returns an absurd value
这是一些结果
In [131]: a
Out[131]: [1, 3, 4, 6, 9, 10, 54]
In [132]: searchsorted(a,1)
Out[132]: 0
In [133]: searchsorted(a,6)
Out[133]: 3
In [134]: searchsorted(a,[9,54,1])
Out[134]: array([4, 6, 0])
In [135]: searchsorted(a,[9,54,1,0])
Out[135]: array([4, 6, 0, 0])
***> # here 0 is not in the list, but turns up @ position 0***
In [136]: searchsorted(a,740)
Out[136]: 7
***> # here 0 is not in the list, but turns up @ position 7***
为什么会这样?
推荐答案
searchsorted
告诉你哪里元素属于保证排序:
searchsorted
tells you where the element belongs to guarantee ordering:
在已排序的数组 a 中查找索引 ,这样,如果 v 中的相应元素插入在索引之前,则 a 的顺序将被保留.
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.
在位置 7 插入 740
将保持排序,就像在位置 0 插入 0 一样.
inserting 740
at position 7 would preserve ordering, as would inserting 0 at position 0.
这篇关于python中的搜索排序是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!