python的算法:二分法查找(2)--bisect模块
Python 有一个 bisect 模块,用于维护有序列表。bisect 模块实现了一个算法用于插入元素到有序列表。在一些情况下,这比反复排序列表或构造一个大的列表再排序的效率更高。Bisect 是二分法的意思,这里使用二分法来排序,它会将一个元素插入到一个有序列表的合适位置,这使得不需要每次调用 sort 的方式维护有序列表。
先看一个最简单的用法:
import bisect l=[1,3,3,6,8,12,15]
x=4 left_insert_point=bisect.bisect_left(l,x)
right_insert_point=bisect.bisect_right(l,x)
print(left_insert_point)
print(right_insert_point)
两个的结果,都是3
import bisect l=[1,3,3,6,8,12,15]
x=3 left_insert_point=bisect.bisect_left(l,x)
right_insert_point=bisect.bisect_right(l,x)
print(left_insert_point)
print(right_insert_point)
结果分别是1,3
bisect的模块不复杂,现在,我们先看bisect_left这个函数:
def bisect_left(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
""" if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo
这里,假定的,就是,a是一个有序的数组。
假设,我们有这么一个数组:
p=[1,4,3,3,6,8,12,15,0,9,3,28] 这实际上,p不是有序的e。 根据bisect_left的算法,每次都是二分。如果查找4的插入位置,当找到3的时候,就停止找了。所以,返回是4. 再看bisect_right:
def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
""" if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
return lo
类似的,不多做描述。
python的算法:二分法查找(2)--bisect模块的更多相关文章
- python bisect 排序模块 二分查找与 bisect 模块
python 3.6.5 import bisect bisect_list=dir(bisect)print(bisect_list)bisect_list = ['__builtins__', ' ...
- 二分查找与 bisect 模块
Python 的列表(list)内部实现是一个数组,也就是一个线性表.在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n).对于大数据量,则可以用二分查找进行优化.二分查找 ...
- C语言的算法--------二分法查找
int find(int n,int a[],int l){int low=0;int high=l-1;int middle=0;while(low<high){middle=(low+hig ...
- python --- 14 递归 二分法查找
一.递归 1.函数自己调用自己 2.官方说明最大深度1000,但跑不到1000,要看解释器, 实测998 3.使⽤递归来遍历各种树形结构 二. 二分法查找 掐头结尾取中间 , 必须是有序序列 ...
- java算法-二分法查找实现
什么是二分法查找 首先,使用二分法查找的前提是:被查找的数组已排好序 具体实现: 假如有一组数为3,12,24,36,55,68,75,88要查给定的值24.可设三个变量front,mid,end分别 ...
- Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...
- python bisect模块二分法查找
#!/usr/bin/env python # encoding: utf-8 import bisect import sys #将一个元素插入到一个有序列表的合适位置 #使用这个模块的函数前先确保 ...
- 查找算法:二分法查找及其python实现案例
承接上一篇:查找:顺序查找与二分法查找,将二分法更多详细的python实现解题写下笔记. 简单方法 ''' 二分法查找在列表中的用户输入值,返回index 三种情况跳出循环体: LR相邻 LR位置重合 ...
- python的算法:二分法查找(1)
1.什么是二分法查找: 1.从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: 2.如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从 ...
随机推荐
- 关闭电脑自带键盘(copy)
用管理员身份运行cmd: 禁用笔记本键盘 sc config i8042prt start= disabled 启用笔记本键盘 sc config i8042prt start= auto
- 【Python】python中的__dict__,__getattr__,__setattr__
Python class 通过内置成员__dict__ 存储成员信息(字典) 首先用一个简单的例子看一下__dict__ 的用法 class A(): def __init__(self,ax,bx) ...
- VS2010历史记录清理
把如下粘贴到文本文件里,另存为批处理文件.(后缀为 *.bat)双击执行就可 @echo off cd \ @echo on @REG Delete HKEY_CURRENT_USER\Softwar ...
- lintcode-106-排序列表转换为二分查找树
106-排序列表转换为二分查找树 给出一个所有元素以升序排序的单链表,将它转换成一棵高度平衡的二分查找树 样例 标签 递归 链表 思路 类似于二分查找,每次将链表二分,中间节点作为根节点,在建立左子树 ...
- iOS-登录发送验证码时60秒倒计时,直接用
__block NSInteger timeout= ; //倒计时时间 KWeakSelf dispatch_queue_t queue = dispatch_get_global_queue(DI ...
- PAT 1020 月饼
https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200 月饼是中国人在中秋佳节时吃的一种传统食品,不 ...
- Jpeg-Baseline和Progressive JPEG的区别
原文来自 http://www.hdj.me/use-progressive-jpeg-in-web 看着不错,于是粘贴了过来 今天才认识到原来JPEG文件有两种保存方式他们分别是Baseline J ...
- AJAX基本演示使用
Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...
- Ubuntu下Eclipse中运行Hadoop程序的参数问题
需要统一的参数: 当配置好eclipse中hadoop的程序后,几个参数需要统一一下: hadoop安装目录下/etc/core_site.xml中 fs.default.name的端口号一定要与ha ...
- BZOJ4602: [Sdoi2016]齿轮 DFS 逆元
这道题就是一个DFS,有一篇奶牛题几乎一样.但是这道题卡精度. 这道题网上的另一篇题解是有问题的.取对数这种方法可以被轻松卡.比如1e18 与 (1e9-1)*(1e9+1)取对数根本无法保证不被卡精 ...