This module has only a few functions. once you decide to use a binary search, you need to use this module 1 instance immediately.
This module has only a few functions,
Once you decide to use binary search, you need to use this module immediately.
Import bisect L = [1, 3, 6, 8, 12, 15] x = 3 x_insert_point = bisect. bisect_left (L, x) # Search for x in L. if x exists, the position on the left of x is returned. if x does not exist, the position to be inserted is returned .. this is 3 that exists in the list and returns the position 1 print x_insert_point = bisect on the left. bisect_right (L, x) # Search for x in L. if x exists, the position on the right of x is returned. if x does not exist, the position to be inserted is returned .. this is where 3 exists in the list and returns the right position 3 print x_insert_point x_insort_left = bisect. insort_left (L, x) # insert x to list L. if x exists, insert it to print L x_insort_ri1_= bisect on the left. insort_right (L, x) # insert x to list L. if x exists, insert it to print L on the right.
Result:
1
3
[1, 3, 3, 3, 6, 8, 12, 15]
[1, 3, 3, 3, 3, 6, 8, 12, 15]
In actual use
The bisect module provides the following functions: (1) search for Bisect. bisect_left (a, x, lo = 0, hi = len (a): search for the index of x inserted in ordered list. Lo and hi are used to specify the range of the list. The entire list is used by default. Bisect. bisect_right (a, x, lo = 0, hi = len (a) bisect. bisect (a, x, lo = 0, hi = len (a) is similar to bisect_left, but if x already exists, insert it on the right. (2) insert bisect. insort_left (a, x, lo = 0, hi = len (a) to insert x into ordered list. If x already exists, insert it on the left. The return value is index. The same effect as a. insert (bisect. bisect_left (a, x, lo, hi), x. Bisect. insort_right (a, x, lo = 0, hi = len (a) bisect. insort (a, x, lo = 0, hi = len (a) is similar to insort_left, but if x already exists, insert it to the right. Functions can be divided into two types, bisect *, used to find the index. Insort * is used for actual insertion. By default, duplicates are inserted from the right. The actual common estimation is insort.