bisect– maintaining an ordered list
Purpose: You do not need to maintain an ordered list every time you call sort.
The Bisect module implements an algorithm for inserting elements into an ordered list. In some cases, this is more efficient than repeating a list or constructing a large list to reorder. Bisect is the meaning of dichotomy, where the binary method is used to sort, and the source code of Bisect is a model of binary ordering. The code for this module is less than 100 lines.
Insert
Import Bisectimport Random # Use aconstant seed to ensure that# the Samepseudo-random numbers# is Usedeach time the L OOP is run.random.seed (1) print ' New Pos Contents ' print '--- -----------' # generaterandom numbers and # Insert Theminto a list in sorted# order.l = []for i inrange (1): Random number of #产生1-100 r = Random.randint (1, +) posit Ion = Bisect.bisect (l, R) Bisect.insort (l, r) print '%3d %3d '% (r, position), l
Execution Result:
#./bisect_example.py
New Pos Contents
--- --- --------
14 0[14]
85 1[14, 85]
77 1[14, 77, 85]
26 1[14, 26, 77, 85]
50 2[14, 26, 50, 77, 85]
45 2[14, 26, 45, 50, 77, 85]
66 4[14, 26, 45, 50, 66, 77, 85]
79 6[14, 26, 45, 50, 66, 77, 79, 85]
10 0[10, 14, 26, 45, 50, 66, 77, 79, 85]
3 0[3, 10, 14, 26, 45, 50, 66, 77, 79, 85]
84 9[3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]
44 4[3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]
77 9[3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
1 0[1, 3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
The functions provided by the Bisect module are:
Bisect.bisect_left (A,x, Lo=0, Hi=len (a)):
Finds the index of the x that is inserted in the ordered list A. Lo and hi are used to specify the interval for the list, and the entire list is used by default. If x already exists, insert it to the left. The return value is index.
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, it is inserted on its right.
Bisect.insort_left (A,x, Lo=0, Hi=len (a))
Insert x in ordered list A. 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))
Similar to Insort_left, but if x already exists, insert it to the right.
Can function can be divided into 2 classes, bisect*, used to find index. Insort* is used for actual insertion. The default repetition is inserted from the right. The actual commonly used estimate is insort.
There is an example in the standard that calculates a rating based on a score:
>>> def grade (score,breakpoints=[60, grades= ' FDCBA '):
... i = bisect (breakpoints, score)
... return grades[i]
...
>>> [Grade (score) for score in [33, 99, 77, 70, 89, 90, 100]]
[' F ', ' A ', ' C ', ' C ', ' B ', ' a ', ' a ']
Bisect does not support keyword parameters like sort, it is recommended to handle the following:
>>> Data =[(' Red ', 5), (' Blue ', 1), (' Yellow ', 8), (' Black ', 0)]>>> Data.sort (key=lambdar:r[1]) >> ;> Keys =[r[1] for R in data] #precomputed list of keys>>> data[bisect_left (keys,0)] (' black ', 0) >>& Gt Data[bisect_left (keys,1)] (' Blue ', 1) >>> Data[bisect_left (keys,5)] (' Red ', 5) >>> Data[bisect_left ( keys,8)] (' Yellow ', 8)