SoFunction
Updated on 2025-03-04

Python3 Maintains the use of ordered list bisect

In Python 3,bisectThe module provides functions for maintaining ordered lists, which are mainly used for binary search and insert operations in ordered sequences. The following is an introduction to its common usage:

1. Import module

First, importbisectModule:

import bisect

2. Main functions and usage

bisect.bisect_left(a, x, lo=0, hi=None)

Function: In ordered listaSearch inxThe position should be inserted to keep the list orderly. If listaExistence andxEqual element, then return the insertion point to the left of the element.

parameter

  • a: Ordered list, which can be an iterable ordered sequence such as a list.
  • x: The element to be inserted.
  • lo: Optional parameter, specify the starting index of the search range, default is 0.
  • hi: Optional parameter, specify the end index of the search range, default to listalength.

Example

a = [1, 2, 4, 4, 6]
x = 4
position = bisect.bisect_left(a, x)
print(position)  
# Output: 2, because 4 should be inserted at the position where the index is 2 (i.e. before the third element) to keep the list in order

bisect.bisect_right(a, x, lo=0, hi=None) (can also be abbreviated as (a, x, lo=0, hi=None))

  • Function: Also in an ordered listaSearch inxThe position should be inserted to keep the list orderly, but if the listaExistence andxEqual element, return the insertion point to the right of the element.
  • parameter:andbisect.bisect_leftThe parameters of   are the same.

Example

a = [1, 2, 4, 4, 6]
x = 4
position = bisect.bisect_right(a, x)
print(position)  
# Output: 4, because 4 should be inserted at the position where index is 4 (i.e. after the fourth element) to keep the list in order

bisect.insort_left(a, x, lo=0, hi=None)

  • Function: In ordered listaFound inxInsert point (usingbisect.bisect_left) and thenxInsert into this position so that the listaStill remain orderly.
  • parameter:andbisect.bisect_leftThe parameters of   are the same.

Example

a = [1, 2, 4, 6]
x = 4
bisect.insort_left(a, x)
print(a)  
# Output: [1, 2, 4, 4, 6], 4 is inserted into the appropriate position, maintaining the order of the list

bisect.insort_right(a, x, lo=0, hi=None) (can also be abbreviated as (a, x, lo=0, hi=None))

  • Function: In ordered listaFound inxInsert point (usingbisect.bisect_right) and thenxInsert into this position so that the listaStill remain orderly.
  • parameter:andbisect.bisect_rightThe parameters of   are the same.
  • Example
a = [1, 2, 4, 6]
x = 4
bisect.insort_right(a, x)
print(a)  
# Output: [1, 2, 4, 4, 6]. Although the result is the same as the result of insert_left insertion 4, the insert logic is different. There will be differences when there are multiple equal elements in the list.

3. Application scenarios

  • Maintain an ordered data set: When you need to constantly add new elements to an ordered data set, and ensure that the data set is always in order,bisectModule functions are very useful. For example, when implementing a dynamically maintained ranking function, you can use it every time a new score is added.The function inserts it in the right position.
  • Find approximate values ​​in binary: If you do not need to find an element accurately, but want to find the location closest to it,bisectFunctions can indirectly achieve this by looking for insertion points. For example, in an ordered temperature record list, find the location closest to the recorded temperature at the given temperature value.

This is the article about the use of Python3 maintaining ordered list bisect. For more related contents of Python3 ordered list bisect, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!