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.如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从 ...
随机推荐
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...
- 使用PHP静态变量当缓存的方法
下面这个PHP的代码实例,功能是帮助用户重置密码,requestResetPassword是接收用户重置密码的请求并且做了相应的检查.为了更好的复用性,我将重置密码的操作单独分配到一个新的resetP ...
- [C/C++] C++声明和定义的区别
·变量定义:用于为变量分配存储空间,还可为变量指定初始值.程序中,变量有且仅有一个定义. ·变量声明:用于向程序表明变量的类型和名字. ·定义也是声明:当定义变量时我们声明了它的类型和名字. ·ext ...
- tomcat 路径"/"表示根目录
- Seajs的用法
以前经常听到Seajs,但是没深入了解过,不清楚到底是用做哪个方面,后来调组到M站做开发,发现项目用到了Seajs,便去了解下 SeaJS是一个遵循CMD规范的JavaScript模块加载框架,可以实 ...
- npm错误总结
You cannot publish over the previously published version 1.0.1." : xxx 发布时一定要修改package.json的版本号 ...
- Website Collection
前一百个卡特兰数 Candy?的博弈论总结 杜教筛资料 线性基资料 (ex)BSGS资料 斐波那契数列前300项 斯特林数 STL标准库-容器-unordered_set C++ unordered_ ...
- [NOIP2012] 文化之旅 dfs
这道题就体现了聪明的搜索策略的重要性,如果我们正着搜,判断效率会明显下滑,所以我们就采用倒着搜索.(其实很玄学.....) #include <cstdio> #include <b ...
- [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能
首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检 ...
- 获得edittext的图片大小
1.在布局文件中编写控件,有2张图片 <EditText android:id="@+id/edit" android:background="@drawable/ ...