Python中bisect的用法及示例详解
作者:咆哮的阿杰 时间:2022-07-06 16:00:12
bisect是python内置模块,用于有序序列的插入和查找。
查找: bisect(array, item)
插入: insort(array,item)
查找
import bisect
a = [1,4,6,8,12,15,20]
position = bisect.bisect(a,13)
print(position)
# 用可变序列内置的insert方法插入
a.insert(position,13)
print(a)
输出:
5
[1, 4, 6, 8, 12, 13, 15, 20]
插入
使用bisect.insort,比bisect先查找该插入哪个位置,再用insert方法插入更加快速的方法
import bisect
a = [1,4,6,8,12,15,20]
bisect.insort(a,13)
print(a)
[1, 4, 6, 8, 12, 13, 15, 20]
NOTE
bisect还有bisect_left,insort_left的用法,和不带left的用法的区别是:当插入的元素和序列中的某一个元素相同时,该插入到该元素的前面(左边,left),还是后面(右边);如果是查找,则返回该元素的位置还是该元素之后的位置。
来源:https://blog.csdn.net/qq_34914551/article/details/100062973
标签:Python,bisect
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python模拟登录百度代码分享(获取百度贴吧等级)
2022-05-13 02:54:34
Pytorch 的 LSTM 模型的示例教程
2021-08-01 22:28:41
命令行运行Python脚本时传入参数的三种方式详解
2021-11-30 00:16:08
Dlib+OpenCV深度学习人脸识别的方法示例
2022-11-08 06:34:42
![](https://img.aspxhome.com/file/2023/3/72063_0s.png)
python opencv检测目标颜色的实例讲解
2021-09-02 01:05:33
![](https://img.aspxhome.com/file/2023/1/103761_0s.jpg)
基于关系型数据库引擎的\\XML\\索引技术
2008-09-05 17:13:00
Sql server 2005 找出子表树
2008-11-24 15:23:00
Python做图像处理及视频音频文件分离和合成功能
2022-07-31 14:58:05
python虚拟环境模块venv使用及示例
2021-07-21 12:05:48
Python中实现输入一个整数的案例
2022-05-28 18:42:21
Python爬虫实现爬取百度百科词条功能实例
2021-01-09 06:23:30
![](https://img.aspxhome.com/file/2023/1/65421_0s.png)
python config文件的读写操作示例
2022-04-05 07:42:11
Python Django框架模板渲染功能示例
2023-03-12 17:39:03
![](https://img.aspxhome.com/file/2023/3/84103_0s.png)
详解Pandas与openpyxl库的超强结合
2021-09-05 03:52:15
![](https://img.aspxhome.com/file/2023/4/120434_0s.png)
一个向上滚动代码
2010-02-10 12:29:00
![](https://img.aspxhome.com/file/UploadPic/20102/10/2010210123159245s.gif)
Django 删除upload_to文件的步骤
2022-03-23 05:47:14
![](https://img.aspxhome.com/file/2023/7/107627_0s.jpg)
深入了解Python中yield from语法的使用
2022-05-18 08:24:17
Python用61行代码实现图片像素化的示例代码
2021-08-27 13:10:55
![](https://img.aspxhome.com/file/2023/7/103297_0s.jpg)
python3 使用ssh隧道连接mysql的操作
2023-09-01 19:15:11
python中使用iterrows()对dataframe进行遍历的实例
2023-12-29 13:43:02
![](https://img.aspxhome.com/file/2023/0/77790_0s.jpg)