Bisect module for binary search, very convenient.
The functions provided by the Bisect module are:
(1) Find Bisect.bisect_left (a,x, Lo=0, Hi=len (a)): Find the index that inserts x in ordered list A. Lo and hi are used to specify the interval for the list, and 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)) These 2 are similar to Bisect_left, but if x already exists, insert it to the right. (2) Insert Bisect.insort_left (a,x, Lo=0, Hi=len (a)) to insert X in ordered list A. If x already exists, insert it to the left. The return value is index. and A.insert (Bisect.bisect_left (a,x, lo, HI), x) have the same effect. Bisect.insort_right (A,x, Lo=0, Hi=len (a)) Bisect.insort (A, x,lo=0, Hi=len (a)) and insort_left are similar, but if x already exists, insert it to the right.
Python Two-point lookup module bisect