In Python 3,bisect
The 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, importbisect
Module:
import bisect
2. Main functions and usage
bisect.bisect_left(a, x, lo=0, hi=None)
Function: In ordered lista
Search inx
The position should be inserted to keep the list orderly. If lista
Existence andx
Equal 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 lista
length.
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 list
a
Search inx
The position should be inserted to keep the list orderly, but if the lista
Existence andx
Equal element, return the insertion point to the right of the element. -
parameter:and
bisect.bisect_left
The 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 list
a
Found inx
Insert point (usingbisect.bisect_left
) and thenx
Insert into this position so that the lista
Still remain orderly. -
parameter:and
bisect.bisect_left
The 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 list
a
Found inx
Insert point (usingbisect.bisect_right
) and thenx
Insert into this position so that the lista
Still remain orderly. -
parameter:and
bisect.bisect_right
The 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,
bisect
Module 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,
bisect
Functions 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!