The use of Python bisec module
I. brief introduction to bisec module:
Python's bisec module is a built-in module, which is used to maintain the sequence table. It uses dichotomy to sort and insert
What are bisec's methods:
# Import bisect package first import bisect print(dir(bisect)) # Output results: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'bisect', 'bisect_left', 'bisect_right', 'insort', 'insort_left', 'insort_right']
Among the above methods, in addition to magic methods, other methods are explained in detail:
Bisec: returns the subscript of the inserted element in the list
my_list = [1, 3, 5, 7, 9] result = bisect.bisect(my_list, 6) print(result) # Output results:3 Indicates the subscript of the insert element in the list
bisect_left: the element is found to the left by default, and the subscript inserted to the left is returned.
my_list = [1, 3, 5, 7, 9] result = bisect.bisect_left(my_list, 6) print(result) # Output results: 3 with bisect Method
Bisec right: opposite to bisec left
my_list = [1, 5, 3, 9, 7] result = bisect.bisect_right(my_list, 6) print(result) # Output results: 3
Insert: inserts elements in the list to the correct location
# If the elements in the list are ordered my_list = [1, 3, 5, 7, 9] bisect.insort(my_list, 2) print(my_list) # Output results:[1, 2, 3, 5, 7, 9] # If the elements in the list are unordered, they are inserted to the far right by default. my_list = [2, 3, 1, 5, 7, 4] bisect.insort(my_list, 6) print(my_list) # Output results:[2, 3, 1, 5, 7, 4, 6]
Insert? Left: insert value on the left, return value None
my_list = [1, 3, 5, 7, 9] bisect.insort_left(my_list, 2) print(my_list) # Output results:[1, 2, 3, 5, 7, 9]
Insert? Right: insert value on the right, return None
my_list = [1, 3, 5, 7, 9] bisect.insort_right(my_list, 6) print(my_list) # Output results:[1, 3, 5, 6, 7, 9]
Insert ﹐ left and insert ﹐ right: just insert in different position
II. Use scenarios:
For example, a chestnut should be graded according to the student's performance. The student's performance (0-100) should be graded into (ABCDEF):
General operation:
def grade(score): if score < 60: return 'F' elif score < 70: return 'E' elif score < 80: return 'D' elif score < 90: return 'C' elif score < 100: return 'B' else: return 'A' scores = [59, 60, 72, 82, 85, 90, 98, 100] result = [grade(item) for item in scores] print(result) # The output is as follows: ['F', 'E', 'D', 'C', 'C', 'B', 'B', 'A']
To use bisec:
-
For example, the dividing point of students' grades is [60, 70, 80, 90, 100], and the grade is' fedba '
def grade(score, points=[60, 70, 80, 90, 100], grade="FEDCBA"): item = bisect.bisect(points, score) return grade[item] scores = [101, 60, 72, 82, 85, 90, 98, 100] result = [grade(item) for item in scores] print(result) # The output results are as follows: ['A', 'E', 'D', 'C', 'C', 'B', 'B', 'A']
-
Store member points for grading:
def grade(score, points=[60, 70, 80, 90, 100], grade="FEDCBA"): item = bisect.bisect(points, score) print(grade[item]) return grade[item] scores = [1000, 2000, 3000] names = ['Ordinary member', 'Intermediate Member', 'Senior member', 'VIP'] values = [999, 1999, 2999, 3001] print([grade(item, points=scores, grade=names) for item in values]) # The output results are as follows:['Ordinary member', 'Intermediate Member', 'Senior member', 'VIP']