numpy的searchsorted细品

 
import numpy as np
a= np.arange(20)
pos_left = a.searchsorted(3)    #也可以写成np .searchsorted(a, 3)
pos_right = a.searchsorted(10, "right") 
pos_left, pos_right     #(3, 11)
a[pos_left : pos_right] #array([ 3,  4,  5,  6,  7,  8,  9, 10]) 注意[:]是左闭右开 
想要利用searchsorted方法来实现获取左右两个端点的数据,则左端点采用"left",右端点"right".

猜你喜欢

转载自www.cnblogs.com/Stephen-Qin/p/10548572.html