1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import bisect
A = [1,2,2.5,3,3.5,4,5] lindex = bisect.bisect_left(A,2.5) rindex = bisect.bisect_right(A,3.5) print(lindex, rindex, rindex-lindex)
def grade(score,breakpoints=[60, 70, 80, 90], grades='FDCBA'): i = bisect.bisect(breakpoints, score) return grades[i]
print([grade(score) for score in [33, 99, 77, 70, 89, 90, 100]])
def binary_search_bisect(lst, x): from bisect import bisect_left i = bisect_left(lst, x) if i != len(lst) and lst[i] == x: return i return None
print(binary_search_bisect(A,4))
|