Jan-03-2020, 06:19 PM
(This post was last modified: Jan-03-2020, 06:20 PM by Gribouillis.)
Assuming that the set of periods is a fixed set of non overlapping intervals and you have potentially many searches to do, it seems to me that the best solution is to use the bisect module
import bisect period = [[1230, 1300], [1400, 1500],[900, 1000], [1530, 1800],[1930, 2100]] # prepare two helper arrays scale, intervals = zip( *sorted((end, inter) for inter in period for end in inter)) scale = list(scale) scale.append(2401) class NotFound(RuntimeError): pass def find_interval(time): idx = bisect.bisect_left(scale, time) if not (idx % 2) and (scale[idx] != time): raise NotFound(time) return intervals[idx] if __name__ == '__main__': i = find_interval(1735) print(i)