python数组查找算法---bisect二分查找插入
来源:互联网 发布:明礼仪知荣辱演讲稿 编辑:程序博客网 时间:2024/04/27 03:17
1 实例
这个模块只有几个函数,
一旦决定使用二分搜索时,立马要想到使用这个模块
- import bisect
- L = [1,3,3,6,8,12,15]
- x = 3
- x_insert_point = bisect.bisect_left(L,x) #在L中查找x,x存在时返回x左侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回左侧位置1
- print x_insert_point
- x_insert_point = bisect.bisect_right(L,x) #在L中查找x,x存在时返回x右侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回右侧位置3
- print x_insert_point
- x_insort_left = bisect.insort_left(L,x) #将x插入到列表L中,x存在时插入在左侧
- print L
- x_insort_rigth = bisect.insort_right(L,x) #将x插入到列表L中,x存在时插入在右侧
- print L
结果:
1
3
[1, 3, 3, 3, 6, 8, 12, 15]
[1, 3, 3, 3, 3, 6, 8, 12, 15]
实际使用中
- bisect.insort_left与 bisect.insort_right 差别不大,作
转自:http://blog.csdn.net/xiaocaiju/article/details/6975714
2 bisect模块
Bisect模块提供的函数有:
(1)查找
bisect.bisect_left(a,x, lo=0, hi=len(a)) :
查找在有序列表a中插入x的index。lo和hi用于指定列表的区间,默认是使用整个列表。
bisect.bisect_right(a,x, lo=0, hi=len(a))
bisect.bisect(a, x,lo=0, hi=len(a))
这2个和bisect_left类似,但如果x已经存在,在其右边插入。
(2)插入
bisect.insort_left(a,x, lo=0, hi=len(a))
在有序列表a中插入x。如果x已经存在,在其左边插入。返回值为index。 和a.insert(bisect.bisect_left(a,x, lo, hi), x) 的效果相同。
bisect.insort_right(a,x, lo=0, hi=len(a))
bisect.insort(a, x,lo=0, hi=len(a))
和insort_left类似,但如果x已经存在,在其右边插入。
可以函数可以分2类,bisect*,用于查找index。Insort*用于实际插入。默认重复时从右边插入。实际常用的估计是insort。
1 0
- python数组查找算法---bisect二分查找插入
- 二分查找--bisect
- git 二分查找 (git bisect)
- python实现二分查找及bisect模块的简介
- [数组]二分查找算法
- python二分查找算法
- 【Python】二分查找算法
- 二分查找算法 python
- Python二分查找算法
- Git bisect 二分查找, 确定问题版本
- python实现查找算法:二分查找法
- python算法之二分查找
- Python实现二分查找算法
- 搜索算法--二分查找(python)
- 二分查找算法python实现
- 5.5 bisect--数组的二分算法
- 数组-二分查找、插入、删除(C版)
- Python查找-二分查找
- Spring MVC 中的基于注解的 Controller
- C#的ArrayList集合的使用
- linux p4使用心得
- mysql5.6主从复制新特性测试
- KMP算法(Python实现)
- python数组查找算法---bisect二分查找插入
- Java之美[从菜鸟到高手演变]之设计模式
- Sysbench参数中文解释
- JavaScript 中小数和大整数的精度丢失
- Bootstrap Paginator分页插件+ajax 实现动态无刷新分页
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
- 阿里巴巴集团全资收购UC优视
- twitter storm源码走读之8 -- TridentTopology创建过程详解 - 徽沪一郎
- Ubuntu 下的sqlmap,nmap,metasploit安装