在Python中可以利用bisect模块来实现二分搜索,该模块包含函数只有几个:

import bisect

L = [1,3,4,5,5,5,8,10]
x = 5 bisect.bisect_left(L,x) #
# 在L中查找x,x存在时返回x最左侧的位置,x不存在返回应该插入的位置 bisect.bisect_right(L,x) #
# 在L中查找x,x存在时返回x最右侧的位置,x不存在返回应该插入的位置 bisect.insort_left(L,x) # [1, 3, 4, 5, 5, 5, 5, 8, 10]
# 将x插入到列表L中,x存在时插入在左侧 bisect.insort_right(L,x) # [1, 3, 4, 5, 5, 5, 5, 5, 8, 10]
# 将x插入到列表L中,x存在时插入在右侧

  bisect.insort_leftbisect.insort_right貌似没什么差别,作用基本一致。

  另外,bisect.bisect(L,x)与bisect_right相同,insort与insort_right相同。

  例子:

import bisect
import random mylist = list()
for i in range(10):
num = random.randint(1,100)
index = bisect.bisect_left(mylist, num)
bisect.insort_left(mylist, num)
print('num ', str(num), 'index ', str(index), 'list ', mylist) mylist = list()
for i in range(10):
num = random.randint(1,100)
index = bisect.bisect_right(mylist, num)
bisect.insort_right(mylist, num)
print('num ', str(num), '\tindex ', str(index), '\tlist ' ,mylist)

  输出:

num  72 index  0 list  [72]
num 89 index 1 list [72, 89]
num 41 index 0 list [41, 72, 89]
num 45 index 1 list [41, 45, 72, 89]
num 100 index 4 list [41, 45, 72, 89, 100]
num 1 index 0 list [1, 41, 45, 72, 89, 100]
num 69 index 3 list [1, 41, 45, 69, 72, 89, 100]
num 4 index 1 list [1, 4, 41, 45, 69, 72, 89, 100]
num 76 index 6 list [1, 4, 41, 45, 69, 72, 76, 89, 100]
num 11 index 2 list [1, 4, 11, 41, 45, 69, 72, 76, 89, 100] num 82 index 0 list [82]
num 39 index 0 list [39, 82]
num 27 index 0 list [27, 39, 82]
num 78 index 2 list [27, 39, 78, 82]
num 9 index 0 list [9, 27, 39, 78, 82]
num 80 index 4 list [9, 27, 39, 78, 80, 82]
num 58 index 3 list [9, 27, 39, 58, 78, 80, 82]
num 42 index 3 list [9, 27, 39, 42, 58, 78, 80, 82]
num 59 index 5 list [9, 27, 39, 42, 58, 59, 78, 80, 82]
num 50 index 4 list [9, 27, 39, 42, 50, 58, 59, 78, 80, 82]

END 2018-11-01 23:25:37

Python学习笔记:bisect模块实现二分搜索的更多相关文章

  1. Python学习笔记之模块与包

    一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...

  2. Python学习笔记—itertools模块

    这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...

  3. python学习笔记_week5_模块

    模块 一.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能), 本质就是.py结尾的python文件(文件名:test.py,对应模块名:test) 包:用来从逻辑上 ...

  4. python学习笔记(八)-模块

    大型python程序以模块和包的形式组织.python标准库中包含大量的模块.一个python文件就是一个模块.1.标准模块 python自带的,不需要你安装的2.第三方模块 需要安装,别人提供的. ...

  5. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  6. Python学习笔记1—模块

    模块的使用 引用模块的两种形式 形式一: import module_name 形式二: from module1 import module11   (module11是module的子模块) 例: ...

  7. Python学习笔记2——模块的发布

    1.为模块nester创建文件夹nester,其中包含:nester.py(模块文件): """这是"nester.py"模块,提供了一个名为prin ...

  8. python学习笔记十——模块与函数

    第五章 模块与函数 5.1 python程序的结构 函数+类->模块              模块+模块->包                 函数+类+模块+包=Python pyth ...

  9. Python学习笔记14—模块

    在python中所有的模块都被加入到了sys.path中,用下面的方法可以看见模块的位置. >>> import sys >>> import pprint > ...

  10. python学习笔记:模块——自定义模块的3种导入方式

    一.定义 模块就是用一堆的代码实现了一些功能的代码的集合,通常一个或者多个函数写在一个.py文件里,而如果有些功能实现起来很复杂,那么就需要创建n个.py文件,这n个.py文件的集合就是模块.如果不懂 ...

随机推荐

  1. 6: Junit1_@Test

    @Test注解是测试的基础,它提供了其他作用 1.指定将会抛出的异常类型,如果没有抛出异常或者抛出的一场不属于我们指定的类型,就会算是测试失败了. @Test(expected = RuntimeEx ...

  2. SpringMVC 上传文件(文件非必填)MultipartHttpServletRequest

    原文:https://blog.csdn.net/dorothy1224/article/details/79136676 上传文件(文件非必填)MultipartHttpServletRequest ...

  3. lumen 使用 laravel-cors 的时候, 使用 dd 函数的解决方法

    if (! function_exists('dd')) { /** * Dump the passed variables and end the script. * * @param mixed ...

  4. zuul学习

    1.zuul可以代理界面所需的后端服务,可以解决CORS(Cross-Origion-Resource-Sharing)和认证问题(authentication)问题 2.zuul是使用ribbon来 ...

  5. Leetcode 503. 下一个更大元素 II

    1.题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应 ...

  6. 百度语音合成 composer

    https://packagist.org/packages/jormin/baidu-speech http://ai.baidu.com/docs#/TTS-Online-PHP-SDK/top

  7. 设置CMD默认路径

    用CMD每一次都得切换路径,很麻烦. 所以,需要设置一下CMD默认路径: 1.打开注册表编辑器(WIN+R打开运行.输入regedit) 2.定位到: “HKEY_CURRENT_USER\Softw ...

  8. Integer两种转int方法比较

    方法一: Integer.parseInt(); 返回的是一个 int 的值. 方法二: new Integer.valueof(); 返回的是 Integer 的对象. new Integer.va ...

  9. Django 2.0.1 官方文档翻译: 文档目录 (Page 1)

    Django documentation contents 翻译完成后会做标记. 文档按照官方提供的内容一页一页的进行翻译,有些内容涉及到其他节的内容,会慢慢补上.所有的翻译内容按自己的理解来写,尽量 ...

  10. WCF: Retry when service is in fault state.

    Service Host: using System; using System.Configuration; using System.ServiceModel; using System.Serv ...