鉴于边界,发现区间 [英] Given boundaries, find interval

查看:101
本文介绍了鉴于边界,发现区间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样一个名单

[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

这篇关于鉴于边界,发现区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆