You can achieve this with the built-in module bisectas follows:
from bisect import bisect
my_list = [17, 30, 62, 65, 92, 95, 98]
my_interval = list(range(0, 101, 10))
new_list = [((i+1, j), len(my_list[bisect(my_list, i+1):bisect(my_list, j)])) \
for i, j in zip(my_interval, my_interval[1:])]
The final hold value will be new_list:
[((0, 10), 0), ((10, 20), 1), ((20, 30), 1), ((30, 40), 0), ((40, 50), 0), ((50, 60), 0), ((60, 70), 2), ((70, 80), 0), ((80, 90), 0), ((90, 100), 3)]
To print the values ββin the desired format, follow these steps:
for (i, j), val in new_list:
print '{}-{}: {}'.format(i, j, val)
which will print:
1-10: 0
11-20: 1
21-30: 1
31-40: 0
41-50: 0
51-60: 0
61-70: 2
71-80: 0
81-90: 0
91-100: 3