I've started using/learning list-comprehensions, as an exercise I decided to use the following code:
def data_idx(data):
if data <= 15:
n = ''
elif data <= 31:
n = 2
elif data <= 47:
n = 3
elif data <= 63:
n = 4
else:
n = 5
return n
I'm wondering, if it is possible to simplify the above code to one line?
I've tried the following approach:
data = 63
limits = [31, 47, 63, 100]
if data > 15:
x = [n+2 for n in range(len(limits)) if limits[n] <= data < limits[n+1]][0]
else:
x = ''
But I don't know how to deal with values <= 15
, to make an output ''
.