学习bisect模块保持列表排序
来源:互联网 发布:淘宝和快递合作多少钱 编辑:程序博客网 时间:2024/05/02 00:20
学习bisect模块保持列表排序
这是一个免费的二分查找实现和快速插入有序序列的工具。也就是说,你可以使用:
>>> import bisect
>>> bisect.insort(list, element)
你已將一个元素插入列表中, 而你不需要再次调用 sort() 来保持容器的排序, 因为这在长序列中这会非常昂贵.
这个模块只有几个函数,一旦决定使用二分搜索时,立马要想到使用这个模块
1.当x在列表中存在时,返回方法中指定的left、right位置的索引或插入在x的左侧或右侧
2.当x在列表中不存在时,返回应该插入的位置索引或插入x中
- 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模块保持列表排序
- python的排序模块bisect
- python的排序模块bisect
- 一个有趣的python排序模块:bisect
- bisect模块
- Python用于排序的模块——bisect
- bisect模块二分插入
- python bisect模块
- python---bisect模块
- Python bisect模块
- python之bisect模块
- python bisect模块
- python中的bisect模块
- Python模块——bisect
- Python模块:bisect二分算法模块
- bisect
- python中bisect模块的使用
- sklearn学习(模块列表)
- adb调试指定设备
- php广告系统优化感想
- 学习CxImage
- web前端开发应该知道的
- String类
- 学习bisect模块保持列表排序
- 状态机的两种写法
- busmouse.h 头文件分析 \linux-1.0\linux\include\linux\busmouse.h
- EntityManager的find()与getReference()的区别
- 折线分割平面 hdu2050 递推
- android 4.0如何打开无线设置界面
- could not find the main class. program will exit问题的解决方法
- 图像工作回顾之一:运动分析
- TLD取经之路(5)-- 此处省略XX字