Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy获取排序数组中插入的索引_Python_Numpy_Insertion - Fatal编程技术网

Python Numpy获取排序数组中插入的索引

Python Numpy获取排序数组中插入的索引,python,numpy,insertion,Python,Numpy,Insertion,我试图找到一种方法来创建一个传递两个数组的函数,其中结果是一个索引数组,第一个数组的值将位于第二个数组中。下面的代码给出了我想要的结果,但我正在尝试摆脱for循环,并找到一种使用numpy函数对其进行矢量化的方法: x_array = np.array([25, 32, 3, 99, 300]) y_array = np.array([30, 33, 56, 99, 250]) result = [0, 1, 0, 3, -1] def get_索引(x_数组,y_数组): 结果=[] 对于

我试图找到一种方法来创建一个传递两个数组的函数,其中结果是一个索引数组,第一个数组的值将位于第二个数组中。下面的代码给出了我想要的结果,但我正在尝试摆脱
for
循环,并找到一种使用numpy函数对其进行矢量化的方法:

x_array = np.array([25, 32, 3, 99, 300])
y_array = np.array([30, 33, 56, 99, 250])

result = [0, 1, 0, 3, -1]
def get_索引(x_数组,y_数组):
结果=[]
对于x_阵列中的x:
index=np。其中(x您正在寻找:

唯一的区别是,如果超过最大元素,则返回数组的大小:

>>> indices
array([0, 1, 0, 3, 5], dtype=int64)
如果需要获得
-1
,可以使用或直接屏蔽:

indices = np.where(indices < y_array.size, indices, -1)
您正在寻找:

唯一的区别是,如果超过最大元素,则返回数组的大小:

>>> indices
array([0, 1, 0, 3, 5], dtype=int64)
如果需要获得
-1
,可以使用或直接屏蔽:

indices = np.where(indices < y_array.size, indices, -1)

第二个数组是否始终排序,并且您正在查找插入索引?第二个数组是否始终排序,并且您正在查找插入索引?
indices[indices >= y_array.size] = -1