This function takes a range in form of "a-b" and generate a list of numbers between a and b inclusive. Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include numbers from a to b, a to d and f Example: hyphen_range('54-78') hyphen_range('57-78,454,45,1-10') hyphen_range('94-100,1052,2-50')
1 2 3 4 5 6 7 8 9 10 11 12 13 | def hyphen_range(s):
""" Takes a range in form of "a-b" and generate a list of numbers between a and b inclusive.
Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include
Numbers from a to b, a to d and f"""
s="".join(s.split())#removes white space
r=set()
for x in s.split(','):
t=x.split('-')
if len(t) not in [1,2]: raise SyntaxError("hash_range is given its arguement as "+s+" which seems not correctly formated.")
r.add(int(t[0])) if len(t)==1 else r.update(set(range(int(t[0]),int(t[1])+1)))
l=list(r)
l.sort()
return l
|
a version using generator.