\$\begingroup\$
\$\endgroup\$
0
I want to search for positions inside a range of integers. How to improve the code in terms of efficiency, since both my search list and the ranges are very big:
#Range list with ids
range_lst = [(121000, 122000, 'foo'), (123456, 124557, 'bar'), (156789, 163659, 'egg')]
# Positions which we want to scan
pos = [123456, 369369]
# Neighbouring windows size
window_size = 10000
for p in pos:
for r in range_lst:
range_area = range(r[0], r[1])
range_area_window = range(r[0]-window_size, r[1]+window_size)
id = r[2]
if p in range_area:
print (p, id, 'exact hit!')
else:
# If we don't find an exact hit, we check the neighbouring region +- 10000
if p in range_area_window:
print (p, id, 'neighbour hit!')
asked Sep 22, 2020 at 8:58
1 Answer 1
\$\begingroup\$
\$\endgroup\$
0
I think that having a range
is not necessary here. If you're only comparing against integer ranges, simple \$ \le \$ and \$ \ge \$ comparisons should suffice.
So, the following function is essentially the same:
def search_position(ranges, positions, window_size=10000):
for low, high, range_id in ranges:
for pos in positions:
if low <= pos < high:
print(pos, range_id, 'exact hit!')
elif (low - window_size) <= pos < (high + window_size):
print(pos, range_id, 'neighbour hit!')
answered Sep 22, 2020 at 15:24
lang-py