python笔记之bisect模块
python笔记之bisect模块
当你决定使用二分搜索时,这个模块会给你带来很大的帮助。
例子
import bisect
L = [1,3,3,6,8,12,15]
x = 3
#在L中查找x,x存在时返回x左侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回左侧位置1
x_insert_point = bisect.bisect_left(L,x)
print x_insert_point #1
#在L中查找x,x存在时返回x右侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回右侧位置3
x_insert_point = bisect.bisect_right(L,x)
print x_insert_point #3
#将x插入到列表L中,x存在时插入在左侧
x_insort_left = bisect.insort_left(L,x)
print L #[1,3,3,3,6,8,12,15]
#将x插入到列表L中,x存在时插入在右侧
x_insort_rigth = bisect.insort_right(L,x)
print L #[1, 3, 3, 3, 3, 6, 8, 12, 15]
#实际使用中bisect.insort_left与 bisect.insort_right 差别不大,作用基本相同
python笔记之bisect模块的更多相关文章
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- python笔记之常用模块用法分析
python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...
- python笔记之itertools模块
python笔记之itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生 ...
- python笔记之ZipFile模块
python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...
- python笔记之subprocess模块
python笔记之subprocess模块 [TOC] 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spaw ...
- python笔记之Cmd模块
python笔记之Cmd模块 Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作.命令行编辑和命令完成. 使用cmd创建的命令行解释器循环读取输入的所有行并 ...
- Python学习笔记:bisect模块实现二分搜索
在Python中可以利用bisect模块来实现二分搜索,该模块包含函数只有几个: import bisect L = [1,3,4,5,5,5,8,10] x = 5 bisect.bisect_le ...
- [Python之路] bisect模块
bisect模块 bisect是Python提供的二分查找模块 源码如下: """Bisection algorithms.""" def ...
- python笔记06-----常用模块(time,os,sys,random)
模块 1. 模块的定义和导入 定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py对应的模块名:test) ...
随机推荐
- 异步编程设计模式Demo - AsyncComponentSample
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 开心菜鸟系列学习笔记--------初探Nodejs(了解篇)
一Node.js开始学习了! 1) 输出hellow worlds a.建一个js文件 hello.js 写 console.info('hellow world !!!'); 进入终 ...
- LeetCode_Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Android Listview异步动态加载网络图片
1.定义类MapListImageAndText管理ListViewItem中控件的内容 package com.google.zxing.client.android.AsyncLoadImage; ...
- Tk::Table
<pre name="code" class="python"># DESCRIPTION # Tk::Table is an all-perl w ...
- 车祸 shit
今天上班的时候就觉得右眼在那跳,妈的,果不其然,回家路上自行车也跟人家撞上了,郁闷,裤子也坏了,腿也伤了.我还没反应过来,撞一起的是个女的,十七八岁吧,郁闷,什么破自行车.强烈呼吁不要去买小自行车了, ...
- SICP 练习 1.3
(define (sum a b) (+ a b)) (define (sum-two a b c) ( cond ((and (> (sum a b) (sum a c)) (> (su ...
- hdu3870-Catch the Theves(平面图最小割)
Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...
- 高精度快速幂(Java版)
import java.io.*; import java.math.*; import java.util.*; import java.text.*; public class Main { pu ...