python bisect 排序模块 二分查找与 bisect 模块
python 3.6.5
import bisect
bisect_list=dir(bisect)print(bisect_list)bisect_list = ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'bisect', 'bisect_left', 'bisect_right', 'insort', 'insort_left', 'insort_right']#一个排序模块,例如:list是进过排序过的sort 一下内容来自:http://python.jobbole.com/86609/
Python 的列表(list)内部实现是一个数组,也就是一个线性表。在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n)。对于大数据量,则可以用二分查找进行优化。二分查找要求对象必须有序,其基本原理如下:
- 1.从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;
- 2.如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。
- 3.如果在某一步骤数组为空,则代表找不到。
二分查找也成为折半查找,算法每一次比较都使搜索范围缩小一半, 其时间复杂度为 O(logn)。
我们分别用递归和循环来实现二分查找:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def binary_search_recursion(lst, value, low, high):
if high < low:
return None
mid = (low + high) / 2
if lst[mid] > value:
return binary_search_recursion(lst, value, low, mid-1)
elif lst[mid] < value:
return binary_search_recursion(lst, value, mid+1, high)
else:
return mid
def binary_search_loop(lst,value):
low, high = 0, len(lst)-1
while low <= high:
mid = (low + high) / 2
if lst[mid] < value:
low = mid + 1
elif lst[mid] > value:
high = mid - 1
else:
return mid
return None
|
接着对这两种实现进行一下性能测试:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
if __name__ == "__main__":
import random
lst = [random.randint(0, 10000) for _ in xrange(100000)]
lst.sort()
def test_recursion():
binary_search_recursion(lst, 999, 0, len(lst)-1)
def test_loop():
binary_search_loop(lst, 999)
import timeit
t1 = timeit.Timer("test_recursion()", setup="from __main__ import test_recursion")
t2 = timeit.Timer("test_loop()", setup="from __main__ import test_loop")
print "Recursion:", t1.timeit()
print "Loop:", t2.timeit()
|
执行结果如下:
Python
1
2
|
Recursion: 3.12596702576
Loop: 2.08254289627
|
可以看出循环方式比递归效率高。
Python 有一个 bisect
模块,用于维护有序列表。bisect
模块实现了一个算法用于插入元素到有序列表。在一些情况下,这比反复排序列表或构造一个大的列表再排序的效率更高。Bisect 是二分法的意思,这里使用二分法来排序,它会将一个元素插入到一个有序列表的合适位置,这使得不需要每次调用 sort 的方式维护有序列表。
下面是一个简单的使用示例:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import bisect
import random
random.seed(1)
print'New Pos Contents'
print'--- --- --------'
l = []
for i in range(1, 15):
r = random.randint(1, 100)
position = bisect.bisect(l, r)
bisect.insort(l, r)
print'%3d %3d' % (r, position), l
|
输出结果:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
New Pos Contents
--- --- --------
14 0 [14]
85 1 [14, 85]
77 1 [14, 77, 85]
26 1 [14, 26, 77, 85]
50 2 [14, 26, 50, 77, 85]
45 2 [14, 26, 45, 50, 77, 85]
66 4 [14, 26, 45, 50, 66, 77, 85]
79 6 [14, 26, 45, 50, 66, 77, 79, 85]
10 0 [10, 14, 26, 45, 50, 66, 77, 79, 85]
3 0 [3, 10, 14, 26, 45, 50, 66, 77, 79, 85]
84 9 [3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]
44 4 [3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]
77 9 [3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
1 0 [1, 3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
|
Bisect模块提供的函数有:
- bisect.bisect_left(a,x, lo=0, hi=len(a)) :
查找在有序列表 a 中插入 x 的index。lo 和 hi 用于指定列表的区间,默认是使用整个列表。如果 x 已经存在,在其左边插入。返回值为 index。
- bisect.bisect_right(a,x, lo=0, hi=len(a))
- bisect.bisect(a, x,lo=0, hi=len(a)) :
这2个函数和 bisect_left 类似,但如果 x 已经存在,在其右边插入。
- bisect.insort_left(a,x, lo=0, hi=len(a)) :
在有序列表 a 中插入 x。和 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 已经存在,在其右边插入。
Bisect 模块提供的函数可以分两类: bisect*
只用于查找 index, 不进行实际的插入;而 insort*
则用于实际插入。该模块比较典型的应用是计算分数等级:
Python
1
2
3
4
5
|
def grade(score,breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect.bisect(breakpoints, score)
return grades[i]
print [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
|
执行结果:
Python
1
|
['F', 'A', 'C', 'C', 'B', 'A', 'A']
|
同样,我们可以用 bisect 模块实现二分查找:
Python
1
2
3
4
5
6
|
def binary_search_bisect(lst, x):
from bisect import bisect_left
i = bisect_left(lst, x)
if i != len(lst) and lst[i] == x:
return i
return None
|
我们再来测试一下它与递归和循环实现的二分查找的性能:
Python
1
2
3
|
Recursion: 4.00940990448
Loop: 2.6583480835
Bisect: 1.74922895432
|
可以看到其比循环实现略快,比递归实现差不多要快一半。
Python 著名的数据处理库 numpy 也有一个用于二分查找的函数 numpy.searchsorted, 用法与 bisect 基本相同,只不过如果要右边插入时,需要设置参数 side='right'
,例如:
Python
1
2
3
4
5
6
7
8
9
10
11
|
>>> import numpy as np
>>> from bisect import bisect_left, bisect_right
>>> data = [2, 4, 7, 9]
>>> bisect_left(data, 4)
1
>>> np.searchsorted(data, 4)
1
>>> bisect_right(data, 4)
2
>>> np.searchsorted(data, 4, side='right')
2
|
那么,我们再来比较一下性能:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
In [20]: %timeit -n 100 bisect_left(data, 99999)
100 loops, best of 3: 670 ns per loop
In [21]: %timeit -n 100 np.searchsorted(data, 99999)
100 loops, best of 3: 56.9 ms per loop
In [22]: %timeit -n 100 bisect_left(data, 8888)
100 loops, best of 3: 961 ns per loop
In [23]: %timeit -n 100 np.searchsorted(data, 8888)
100 loops, best of 3: 57.6 ms per loop
In [24]: %timeit -n 100 bisect_left(data, 777777)
100 loops, best of 3: 670 ns per loop
In [25]: %timeit -n 100 np.searchsorted(data, 777777)
100 loops, best of 3: 58.4 ms per loop
|
可以发现 numpy.searchsorted 效率是很低的,跟 bisect 根本不在一个数量级上。因此 searchsorted 不适合用于搜索普通的数组,但是它用来搜索 numpy.ndarray 是相当快的:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
|
In [30]: data_ndarray = np.arange(0, 1000000)
In [31]: %timeit np.searchsorted(data_ndarray, 99999)
The slowest run took 16.04 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 996 ns per loop
In [32]: %timeit np.searchsorted(data_ndarray, 8888)
The slowest run took 18.22 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 994 ns per loop
In [33]: %timeit np.searchsorted(data_ndarray, 777777)
The slowest run took 31.32 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 990 ns per loop
|
numpy.searchsorted
可以同时搜索多个值:
Python
1
2
3
4
5
6
|
>>> np.searchsorted([1,2,3,4,5], 3)
2
>>> np.searchsorted([1,2,3,4,5], 3, side='right')
3
>>> np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3])
array([0, 5, 1, 2])
|
python bisect 排序模块 二分查找与 bisect 模块的更多相关文章
- cb16a_c++_顺序容器的选用_排序_二分查找
/*cb16a_c++_顺序容器的选用_排序_二分查找顺序容器: 1.vector的优点与缺点 vector优点:排序利用下标,快速排序,做二分查找非常快 2.list的优点与缺点 list优点:插入 ...
- 二分查找与 bisect 模块
Python 的列表(list)内部实现是一个数组,也就是一个线性表.在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n).对于大数据量,则可以用二分查找进行优化.二分查找 ...
- 使用bisect库实现二分查找
手动实现 假如有一个有序表nums,怎么样在nums里找到某个值的位置呢?没错,就是nums.index(k),哈哈哈哈哈哈哈-- 假如nums很长很长,那就要祭出二分查找了 def binary_s ...
- Java基础【冒泡、选择排序、二分查找】
冒泡排序的思路就是前一个和后一个进行比较,如果大的就交换位置 大的数字后浮 如 12 8 5 31 第一轮 8 5 12 31 第二轮 5 8 ...
- java-冒泡排序、选择排序、二分查找
1.冒泡排序 public void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { //外循环只需要比较a ...
- StringBuffer、StringBuilder、冒泡与选择排序、二分查找、基本数据类型包装类_DAY13
1:数组的高级操作(预习) (1)数组:存储同一种数据类型的多个元素的容器. (2)特点:每个元素都有从0开始的编号,方便我们获取.专业名称:索引. (3)数组操作: A:遍历 public stat ...
- 【Java数据结构与算法】简单排序、二分查找和异或运算
简单排序 选择排序 概念 首先,找到数组中最小的那个元素,其次,把它和数组的第一个元素交换位置(如果第一个元素就是最小的元素那么它就和自己交换).再次,在剩下的元素中找到最小的元素,将它与数组的第二个 ...
- sort排序与二分查找
#include<iostream> #include<vector> #include<algorithm> #include<string> usi ...
- 算法:二分查找(python版)
#!/usr/bin/env python #coding -*- utf:8 -*- #二分查找#时间复杂度O(logn)#一个时间常量O(1)将问题的规模缩小一半,则O(logn) import ...
随机推荐
- Android 开发进入Linux系统执行命令 2018-5-25 Fri.
/** * 进入linux cmd执行命令 * * @param command * @return */ private boolean runRootCommand(String command) ...
- HTML 视频
在html5中使用视频,只要添加元素<video></video>元素表示. <video>有几个属性: src 指定音频文件的路径 poster 视频播放之前显示 ...
- yii 缓存的使用 以及使用需要开启php的apc扩展
public function behaviors() { return [ [ 'class' => 'yii\filters\PageCache', 'only' => ['index ...
- boot中 Quartz注入spring管理类失败
在项目中用到了Quartz,想在里面实现业务操作发现sping类注入总是失败.后来网上查询了一下解决办法.下面把我成功解决问题的这个版本发出来,大家一起学习一下. 在quartz 会发现 job中无法 ...
- CXF使用JMS作为传输协议的配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- grabcut 分割 Rect
#include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src = imread("E:\ ...
- 什么是webFlux
什么是webFlux 左侧是传统的基于Servlet的Spring Web MVC框架,右侧是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,从上到下依次是R ...
- asp.net mvc areas
http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC
- EVIL TWIN AP
git clone https://github.com/P0cL4bs/3vilTwinAttacker.git cd 3vilTwinAttacker/ ./installer.sh --inst ...
- JMeter一次简单的接口测试(转载)
转载自 http://www.cnblogs.com/yangxia-test 本次接口测试:根据ws查询所有商品的具体的信息.检查商品是否返回成功. 1. 准备测试数据 查询数据库中产品表已上架商 ...