Python: Mapping from intervals to values

前端 未结 6 966
遥遥无期
遥遥无期 2020-11-30 00:58

I\'m refactoring a function that, given a series of endpoints that implicitly define intervals, checks if a number is included in the interval, and then return a correspondi

6条回答
  •  孤街浪徒
    2020-11-30 01:18

    Another way ...

    def which(lst, p): 
        return len([1 for el in lst if p > el])
    
    lst = [100, 300, 500, 800, 1000]
    which(lst, 2)
    which(lst, 101)
    which(lst, 1001)
    

提交回复
热议问题