How to find in what range is the number?

There is an ordered list:
`l = [100, 200, 300, 400, ,500]`
The function gets the number of a.
You need to establish what range is the number and assign a y value.
``````if a <= 100:
y = 0
elif a <= 200:
y = 1
elif a <= 300:
y = 2
elif a <= 400:
y = 3
else:
y = 4``````

How can you not do such a clumsy way through the conditions? using a function that will find the list range and assign a value.
March 23rd 20 at 18:59
March 23rd 20 at 19:01
Solution
My version. A list of built-in function, the input received is strictly "a". Everything will be more Penultimate element written in the last group, even if greater than the last element.

``````def group_place(a):
l = [100, 200, 300, 400, 500]
for i in range(0, len(l)):
if a <= l[i]: break
return i``````
March 23rd 20 at 19:03
Solution
For short lists it can be easy to get around all the elements.

``````def get_group(value, sorted_list):
for i breakpoint in enumerate(sorted_list):
if value < breakpoint:
return i
# a value greater than the last in the list
return len(sorted_list)

l = [100, 200, 300, 400, 500]
print(get_group(215, l))``````

For very long list is a preliminary build list binary tree to reduce the number of inspections.
March 23rd 20 at 19:05
A binary search of the standard library.
``````import bisect
print(bisect.bisect_left(l, a))``````
March 23rd 20 at 19:07
``````def r(a, l):
b = 0
for i, d in enumerate(l):
if a > d:
b = i + 1
return b``````
March 23rd 20 at 19:09
you can try:
``````l = [100, 200, 300, 400, 500]
x = 288
print(min([ind for ind, i in enumerate(l) if i > x]))``````
March 23rd 20 at 19:11
`(value - 1)/100`
March 23rd 20 at 19:13
Example https://coderun.ru/prostye-otvety/kak-najjti-diapa... but in PHP, the principle should be clear

Find more questions by tags Python