Python bisect module and pythonbisect Module
The bisect module of Python is a built-in module. The bisect module implements an algorithm used to insert elements to an ordered list.
In some cases, this is more efficient than repeatedly sorting a list or constructing a large list and then sorting. Bisect is the meaning of the binary method, where binary sorting is used to insert the elements to be inserted to a proper position.
The bisect module provides the following functions:
(Lo and hi in the following function are used to specify the range of the list. The default value is the entire list)
Bisect. bisect_left (a, x, lo = 0, hi = len (a) returns the position where x is inserted into the index in list a. If x already exists, the position of the first x is returned.
Bisect. bisect_right (a, x, lo = 0, hi = len (a) returns x inserted to the index position in list a. If x already exists, returns the next position of the last x position.
Bisect. bisect (a, x, lo = 0, hi = len (a) is the same as bisect_right.
Bisect. insort_left (a, x, lo = 0, hi = len (a) insert x to list a. If there is already x, insert it to the leftmost of all x
Bisect. insort_right (a, x, lo = 0, hi = len (a) insert x to list a. If there is already x, insert it to the rightmost of all x
Bisect. insort (a, x, lo = 0, hi = len (a) is the same as insort_right
The following is an example:
>>> import bisect>>> import random>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect_left(mylist, num)bisect.insort_left(mylist, num)print('num ', str(num), '\tindex ', str(index), '\tlist ', mylist)num 37 index 0 list [37]num 31 index 0 list [31, 37]num 82 index 2 list [31, 37, 82]num 27 index 0 list [27, 31, 37, 82]num 62 index 3 list [27, 31, 37, 62, 82]num 83 index 5 list [27, 31, 37, 62, 82, 83]num 83 index 5 list [27, 31, 37, 62, 82, 83, 83]num 87 index 7 list [27, 31, 37, 62, 82, 83, 83, 87]num 98 index 8 list [27, 31, 37, 62, 82, 83, 83, 87, 98]num 99 index 9 list [27, 31, 37, 62, 82, 83, 83, 87, 98, 99]>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect_right(mylist, num)bisect.insort_right(mylist, num)print('num ', str(num), '\tindex ', str(index), '\tlist ', mylist)num 85 index 0 list [85]num 73 index 0 list [73, 85]num 82 index 1 list [73, 82, 85]num 36 index 0 list [36, 73, 82, 85]num 80 index 2 list [36, 73, 80, 82, 85]num 4 index 0 list [4, 36, 73, 80, 82, 85]num 79 index 3 list [4, 36, 73, 79, 80, 82, 85]num 6 index 1 list [4, 6, 36, 73, 79, 80, 82, 85]num 64 index 3 list [4, 6, 36, 64, 73, 79, 80, 82, 85]num 25 index 2 list [4, 6, 25, 36, 64, 73, 79, 80, 82, 85]>>> mylist = list()>>> for i in range(10):num = random.randint(1,100)index = bisect.bisect(mylist, num)bisect.insort(mylist, num)print('num ', str(num), '\tindex ', str(index), '\tlist ', mylist)num 25 index 0 list [25]num 28 index 1 list [25, 28]num 81 index 2 list [25, 28, 81]num 9 index 0 list [9, 25, 28, 81]num 29 index 3 list [9, 25, 28, 29, 81]num 66 index 4 list [9, 25, 28, 29, 66, 81]num 34 index 4 list [9, 25, 28, 29, 34, 66, 81]num 60 index 5 list [9, 25, 28, 29, 34, 60, 66, 81]num 61 index 6 list [9, 25, 28, 29, 34, 60, 61, 66, 81]num 3 index 0 list [3, 9, 25, 28, 29, 34, 60, 61, 66, 81]