Given a list of numbers, for example:
lst = [0, 10, 15, 17]
I need a list containing elements from i -> i + 3 for all i in lst . If there are overlapping ranges, I would like them to be combined.
So, in the above example, we first get:
[0, 1, 2, 3, 10, 11, 12, 13, 15, 16, 17, 18, 17, 18, 19, 20]
But for the last two groups, the ranges overlap, so when you combine them, you have:
[0, 1, 2, 3, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20]
This is my desired result.
This is what I was thinking:
from collections import OrderedDict res = list(OrderedDict.fromkeys([y for x in lst for y in range(x, x + 4)]).keys()) print(res) = [0, 1, 2, 3, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20]
However, this is slow ( 10000 loops, best of 3: 56 µs per loop ). I would like it to be possible with a numpy solution or a python solution that would be faster than that.