def expand_numbers(s, nmin=1, nmax=100): a = [] for v in re.split('[\s,]+', s): if '-' in v: v0, v1 = v.split('-')[:2] if v0 == '': v0 = nmin if v1 == '': v1 = nmax a.extend(range(int(v0), int(v1)+1)) else: a.append(int(v)) return list(set(a))
>>> expand_numbers('3,5,7-10') [3, 5, 7, 8, 9, 10] >>> expand_numbers('-5,8') [1, 2, 3, 4, 5, 8] >>> expand_numbers('3-5,4-7') [3, 4, 5, 6, 7]