Python中bisect模块用法,及实现方式
2015-09-28 22:24
706 查看
#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;
相关文章推荐
- numpy教程 - 矩阵及其运算
- python3.4 + django1.8的环境搭建
- python模拟登录网易邮箱
- Python3+Opencv3的安装
- python实现文件查找
- DDMS
- Python - 缩写(capwords) 和 创建转换表(maketrans) 详细说明
- Python进阶 - HTML获取与解析
- python中logging包的使用小结
- python 批量ping一个网段 返回结果 的程序
- python 操作json
- python模拟浏览器登录人人网,并使用代理IP和发送表单数据
- LeetCode----Kth Largest Element in an Array
- python简单图片转字符
- Python: NLTK几个入门函数
- 【python】matplotlib在windows10 和win7下安装
- python抓取网站URL小工具
- python web开发框架django安装和测试
- @修饰符--python中的装饰器
- Python 脚本学习笔记(五)集中式病毒扫描,端口扫描以及分段数据库操作