有这样一个名单
[207, 357, 470, 497, 537]
每个数字表示一个区间的边界( 0
是隐含在列表的开头),什么是一个pythonic的方法的找出哪个区间给定数目 N
属于?
where each number denotes the boundary of an interval (0
being implicit at the beginning of the list), what is a pythonic way of finding out to which interval a given number n
belongs to?
因此,间隔
0: (0, 207)
1: (208, 357)
2: (358, 497)
3: (498, 537)
如果 N = 0
,那么相应的时间间隔为 0
为 N = 360
,它是 2
。
If n=0
, then the corresponding interval is 0
, for n=360
, it's 2
.
使用 开张
课程模块:
Using the bisect
module of course:
>>> import bisect
>>> lst = [207, 357, 470, 497, 537]
>>> bisect.bisect_left(lst, 0)
0
>>> bisect.bisect_left(lst, 360)
2