Python中bisect模块用法,及实现方式
#bisect用法:
import bisect
bisect.bisect_left(t,x) #在T列表中查找x,若存在,返回x左侧位置
bisect.bisect_right(t,x)
bisect.insort_left(t,x) #在T列表中查找X,若存在,插入x左侧;
bisect.insort_right(t,x)
下面是其实现的方法,实际是二分法:
def binary_search(t,x):
temp = t;
temp.sort();
low = 0;
mid = 0;
high = len(temp)-1;
while low < high:
mid = (low+high)/2;
if x<t[mid]:
high = mid-1;
elif x>t[mid]:
low = mid+1;
else:
return mid-1; #是否等价与bisect_left;
阅读更多
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了