bisect_left,bisect_right,bisect的用法,区别以及源码分析

简介: bisect_left,bisect_right,bisect的用法,区别和源码分析

bisect_left(args, *kwargs)

向一个数组插入一个数字,返回应该插入的位置。
如果这个数字不存在于这个数组中,则返回第一个比这个数大的数的索引
如果这个数字存在,则返回数组中这个数的位置的最小值(即最左边那个索引)

案例1:这个数在数组中不存在

arr = [1, 3, 3, 5, 6, 6, 7, 9, 11]

# 在已排序的列表中查找元素 6 的插入位置
index = bisect_left(arr, 5.5)

print(f"Insert 6 at index {index} to maintain sorted order.")

执行结果:

Insert 6 at index 4 to maintain sorted order.

我们找到了第一个大于5.5的数字6的位置,输出4

案例2:这个数在数组中存在

我们修改一下代码,寻找6的位置

from bisect import bisect_left, bisect, bisect_right

arr = [1, 3, 3, 5, 6, 6, 7, 9, 11]

# 在已排序的列表中查找元素 6 的插入位置
index = bisect_left(arr, 6)

print(f"Insert 6 at index {index} to maintain sorted order.")

执行结果:

Insert 6 at index 4 to maintain sorted order.

我们发现还是4

bisect_right(args, *kwargs)

向一个数组插入一个数字,返回应该插入的位置。
作用:返回第一个比这个数大的数的索引

案例1:这个数在数组中不存在

from bisect import bisect_left, bisect, bisect_right

arr = [1, 3, 3, 5, 6, 6, 7, 9, 11]

# 在已排序的列表中查找元素 6 的插入位置

index = bisect_right(arr, 6.5)

print(f"Insert 6 at index {index} to maintain sorted order.")

执行结果:

Insert 6 at index 6 to maintain sorted order.

数字7的位置被输出

案例2:这个数在数组中存在

from bisect import bisect_left, bisect, bisect_right

arr = [1, 3, 3, 5, 6, 6, 7, 9, 11]

# 在已排序的列表中查找元素 6 的插入位置

index = bisect_right(arr, 6)

print(f"Insert 6 at index {index} to maintain sorted order.")

执行结果:

Insert 6 at index 6 to maintain sorted order.

数字7的位置被输出

对比bisect_left 和 bisect_right

相同点:
当第二个参数数字x不在第一个参数数组arr中时候,二者都会返回arr中第一个比x大的数的位置

不同点:
当arr中存在x,bisect_left会返回arr中x的最小索引,而bisect_right会返回第一个比x大的数的位置

bisect()

alt

完整代码

from bisect import bisect_left, bisect, bisect_right

arr = [1, 3, 3, 5, 6, 6, 7, 9, 11]

# 在已排序的列表中查找元素 6 的插入位置

index = bisect_right(arr, 6)

print(f"Insert 6 at index {index} to maintain sorted order.")

index = bisect(arr, 6)

print(f"Insert 6 at index {index} to maintain sorted order.")


index = bisect_left(arr, 6)

print(f"Insert 6 at index {index} to maintain sorted order.")

结果:

Insert 6 at index 6 to maintain sorted order.
Insert 6 at index 6 to maintain sorted order.
Insert 6 at index 4 to maintain sorted order.

源码分析

我们先来看 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
        # Use __lt__ to match the logic in list.sort() and in heapq
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo

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
        # Use __lt__ to match the logic in list.sort() and in heapq
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

我们观察到这两种实现方式主要在于下面两行:
bisect_right:

        if x < a[mid]: hi = mid
        else: lo = mid+1

bisect_left:

        if a[mid] < x: lo = mid+1
        else: hi = mid

我们观察到,两段源码都会返回lo,即左边界,所以我们关注一下这两行代码对于左边界的影响:
在bisect_right中,只有当 x=a[mid] or x>a[mid]时,lo才会更新为mid+1,所以最终的lo只可能是第一个大于x的索引
在bisect_left中,当 a[mid] < x时,lo会更新为mid+1,此时我们想要的索引位置必然在mid右侧,所以lo可以为相同的x的第一次出现的位置;同时我们注意到当 a[mid] >= x时,hi=mid,说明当lo取到了相同的数的最左侧时,hi右端点其实会向左平移的,所以lo既可以是数组中第一个大于x的数的索引,也可以是相同的x的最左侧第一个x的索引

目录
相关文章
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
1464 4
|
存储 缓存 NoSQL
MySQL索引详解(一文搞懂)
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。
50523 17
MySQL索引详解(一文搞懂)
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
自注意力机制在Transformer中备受瞩目,看似‘主角’,为何FFN却在背后默默扮演关键角色?
本文三桥君深入解析Transformer模型中的前馈全连接层(FFN)机制,揭示其通过两层线性变换和ReLU激活增强模型表达能力的关键作用。文章从输入准备、结构原理到计算过程进行详细阐述,并提供PyTorch实现代码。同时探讨了FFN的优化方向及与自注意力机制的协同效应,为AI从业者提供实践建议。AI专家三桥君结合图文并茂的讲解方式,帮助读者掌握这一影响Transformer性能的核心组件。
1591 0
vllm+vllm-ascend本地部署QwQ-32B
本指南介绍如何下载、安装和启动基于Ascend的vLLM模型。首先,可通过华为镜像或Hugging Face下载预训练模型;其次,安装vllm-ascend,支持通过基础镜像(如`quay.io/ascend/vllm-ascend:v0.7.3-dev`)或源码编译方式完成;最后,使用OpenAI兼容接口启动模型,例如运行`vllm serve`命令,设置模型路径、并行规模等参数。适用于大模型推理场景,需注意显存需求(如QwQ-32B需70G以上)。
5036 17
|
数据采集 人工智能 监控
40.8K star!让AI帮你读懂整个互联网:Crawl4AI开源爬虫工具深度解析
Crawl4AI 是2025年GitHub上备受瞩目的开源网络爬虫工具,专为AI时代设计。它不仅能抓取网页内容,还能理解页面语义结构,生成适配大语言模型的训练数据格式。上线半年获4万+星标,应用于1200+AI项目。其功能亮点包括智能内容提取引擎、AI就绪数据管道和企业级特性,支持动态页面处理、多语言识别及分布式部署。技术架构基于Python 3.10与Scrapy框架,性能卓越,适用于AI训练数据采集、行业情报监控等场景。相比Scrapy、BeautifulSoup等传统工具,Crawl4AI在动态页面支持、PDF解析和语义分块方面更具优势
5000 0
40.8K star!让AI帮你读懂整个互联网:Crawl4AI开源爬虫工具深度解析
|
机器学习/深度学习 搜索推荐 算法
深度学习推荐模型-DIN
Deep Interest Network(DIN)是盖坤大神领导的阿里妈妈的精准定向检索及基础算法团队,在2017年6月提出的。 它针对电子商务领域(e-commerce industry)的CTR预估,重点在于充分利用/挖掘用户历史行为数据中的信息。
1709 1
深度学习推荐模型-DIN