How to do an inverse `range`, i.e. create a compact range based on a set of numbers?
问题 Python has a range method, which allows for stuff like: >>> range(1, 6) [1, 2, 3, 4, 5] What I’m looking for is kind of the opposite: take a list of numbers, and return the start and end. >>> magic([1, 2, 3, 4, 5]) [1, 5] # note: 5, not 6; this differs from `range()` This is easy enough to do for the above example, but is it possible to allow for gaps or multiple ranges as well, returning the range in a PCRE-like string format? Something like this: >>> magic([1, 2, 4, 5]) ['1-2', '4-5'] >>>