Motivation:
A list of [1,2,3,4,5,6,7,8,9,10]
is expressible as range(1,11)
and in
checks are near constant time computations if checked against ranges. Big-ish lists of numbers can be simplified if expressed as list of range()
objects using fewer bytes then used by storing all of them as numbers in a list.
# what I have:
numbers = [1, 2, 5, 7, 8, 9, 10, 13, 14, 15, 42, 43, 44, 45, 46, 47, 48, 49, 50]
# what I want:
ranges = [range(1,3), range(5,6), range(7,11), range(13,16), range(42,51)]
(Self answered) Question:
How can I move from a list of numbers to a list of ranges to facilitate smaller storage and faster in
checks - for consecutive numbers?
Inspired by
- Find index of longest continious sequence in list python
- How to transform a list of numbers into a list of ranges of consecutive numbers (which would take the ranges and string-format them)
- my answer to Concatenate and simplify a list containing number and letter pairs where I could have used this to merge consecutive
ord(letter)
's of"abcd"
into"a-d"