In Python, how do you find the index of the first value greater than a threshold in a sorted list?

前端 未结 3 850
走了就别回头了
走了就别回头了 2020-12-01 10:29

In Python, how do you find the index of the first value greater than a threshold in a sorted list?

I can think of several ways of doing this (linear search, hand-wri

3条回答
  •  情书的邮戳
    2020-12-01 11:04

    Related Index and Val of the last element greater than a threshold

    l = [1, 4, 9, 16, 25, 36, 49, 64, 100, 81, 100]
    max((x,i) for i, x in enumerate(l) if x > 4)
    (100, 10)
    

提交回复
热议问题