What would be the most pythonesque way to find the first index in a list that is greater than x?
For example, with
list = [0.5, 0.3, 0.9, 0.8]
The function
f(list, 0.7)
would return
2.
What would be the most pythonesque way to find the first index in a list that is greater than x?
For example, with
list = [0.5, 0.3, 0.9, 0.8]
The function
f(list, 0.7)
would return
2.
>>> f=lambda seq, m: [ii for ii in xrange(0, len(seq)) if seq[ii] > m][0]
>>> f([.5, .3, .9, .8], 0.7)
2
for index, elem in enumerate(elements):
if elem > reference:
return index
raise ValueError("Nothing Found")
>>> alist= [0.5, 0.3, 0.9, 0.8]
>>> [ n for n,i in enumerate(alist) if i>0.7 ][0]
2
if list is sorted then bisect_left(alist, value)
is faster for a large list than next(i for i, x in enumerate(alist) if x >= value)
.