我正在使用numpy
searchsort()
函数来查找numpy
数组的索引。它仅适用于某些阵列。出了什么问题(下面的实现)?你知道吗
import numpy as np
#specify the dtype in RV
RV = np.array([
np.array([0.23, 2.5, 5.0, 7.1]),
np.array(['a1', 'a2']),
np.array(['b2', 'b1'])
], dtype=object)
print(RV)
def Rules():
global r
r = np.array(np.meshgrid(*RV), dtype=object).T.reshape(-1,len(RV))
return r
Rules()
print(r)
print(RV[0].searchsorted(r[:,0])) #working
print(RV[1].searchsorted(r[:,1])) #working
print(RV[2].searchsorted(r[:,2])) #not working
默认情况下,
searchsorted()
的array
参数必须是排序的参数。因此,解决方案是:使用
numpy.sort()
提前排序:或者使用
sorter
参数:相关问题 更多 >
编程相关推荐