python bisect

本文详细介绍了Python标准库中bisect模块的四个核心函数:bisect_left、bisect_right、insort_left和insort_right。这些函数用于在已排序的列表中查找元素的插入位置或直接插入元素,同时保持列表的有序状态。文章解释了每个函数的功能、参数及返回值,并说明了当目标元素已经存在于列表中时,函数如何定位插入点。

import bisect

这个库只有四个函数

左或右+插入或索引

  1. bisect_left(a, x[, lo[, hi]])
  2. insort_left(a, x[, lo[, hi]])

把帮助手册整理下贴在下面


            
    bisect_left(...)
        bisect_left(a, x[, lo[, hi]]) -> index
        
        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, i points 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.
    
    bisect_right(...)
        bisect_right(a, x[, lo[, hi]]) -> index
        
        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, i points just
        beyond the rightmost x already there
        
        Optional args lo (default 0) and hi (default len(a)) bound the
        slice of a to be searched.
    
    
    insort_left(...)
        insort_left(a, x[, lo[, hi]])
        
        Insert item x in list a, and keep it sorted assuming a is sorted.
        
        If x is already in a, insert it to the left of the leftmost x.
        
        Optional args lo (default 0) and hi (default len(a)) bound the
        slice of a to be searched.
    
    insort_right(...)
        insort_right(a, x[, lo[, hi]])
        
        Insert item x in list a, and keep it sorted assuming a is sorted.
        
        If x is already in a, insert it to the right of the rightmost x.
        
        Optional args lo (default 0) and hi (default len(a)) bound the
        slice of a to be searched.
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值