当前位置:   article > 正文

【Python】 基于二分的查找和插入模块 bisect (示例+源码)_bisect_left源码

bisect_left源码

模块名

bisect

使用这个模块的函数前,需要先确保操作的列表是已排序的。


查找函数

二分查找item 插入 list 时,插入位置的下标。并不实际执行插入操作。

bisect_left(list, item) :如果 list 中存在 item ,则返回插入到这些 item 左侧 后的的下标。

bisect(list, item)bisect_right(list, item) :如果 list 中存在 item ,则返回插入到这些 item 右侧 后的的下标。


插入函数

通过二分查找查找插入位置,并执行插入操作,使得操作后的列表保持有序。

insort_left(list, item) :如果 list 中存在 item ,则插入到这些 item左侧

insort(list, item)insort_right(list, item) :如果 list 中存在 item ,则插入到这些 item右侧


代码示例

from bisect import *

l = [3, 6, 8, 8, 10]

# 查找
bisect_left(l, 1)		# 返回值:0
bisect_left(l, 7)		# 返回值:2
bisect_left(l, 8)		# 返回值:2
bisect_left(l, 15)		# 返回值:5

bisect_right(l, 1)		# 返回值:0
bisect_right(l, 7)		# 返回值:2
bisect_right(l, 8)		# 返回值:4
bisect_right(l, 15)		# 返回值:5

# 插入
l = [6, 8, 8, 10]
insort_left(l, 1)		# l = [1, 6, 8, 8, 10]
insort_left(l, 7)		# l = [1, 6, 7, 8, 8, 10]
insort_left(l, 8.0)		# l = [1, 6, 7, 8.0, 8, 8, 10]
insort_left(l, 15)		# l = [1, 6, 7, 8.0, 8, 8, 10, 15]

l = [6, 8, 8, 10]
insort_right(l, 1)		# l = [1, 6, 8, 8, 10]
insort_right(l, 7)		# l = [1, 6, 7, 8, 8, 10]
insort_right(l, 8.0)	# l = [1, 6, 7, 8, 8, 8.0, 10]
insort_right(l, 15)		# l = [1, 6, 7, 8, 8, 8.0, 10, 15]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

模块源码

"""Bisection algorithms."""

def insort_right(a, x, lo=0, hi=None):
    """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.
    """

    lo = bisect_right(a, x, lo, hi)
    a.insert(lo, x)

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
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo

def insort_left(a, x, lo=0, hi=None):
    """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.
    """

    lo = bisect_left(a, x, lo, hi)
    a.insert(lo, x)


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
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

# Overwrite above definitions with a fast C implementation
try:
    from _bisect import *
except ImportError:
    pass

# Create aliases
bisect = bisect_right
insort = insort_right
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/152918
推荐阅读
相关标签
  

闽ICP备14008679号