0%

python 的二分查找库:bisect

具体参考 文章

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))

支持一根棒棒糖!