This version maneja espacios en blanco arbitraria, intervalos que se solapan, los intervalos fuera de orden, y números enteros negativos:
from itertools import chain
def group_to_range(group):
group = ''.join(group.split())
sign, g = ('-', group[1:]) if group.startswith('-') else ('', group)
r = g.split('-', 1)
r[0] = sign + r[0]
r = sorted(int(__) for __ in r)
return range(r[0], 1 + r[-1])
def rangeexpand(txt):
ranges = chain.from_iterable(group_to_range(__) for __ in txt.split(','))
return sorted(set(ranges))
>>> rangeexpand('-6,-3--1,3-5,7-11,14,15,17-20')
[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]
>>> rangeexpand('1-4,6,3-2, 11, 8 - 12,5,14-14')
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 14]
un duplicado. http://stackoverflow.com/questions/712460/interpreting-number-ranges-in-python –