I have \"2,5,7-9,12\"
string.
I want to get [2, 5, 7, 8, 9, 12] list from it.
Is there any built-in function for it in python?
Thanks.
I am not aware of any built-in function that would do that. The following isn't particularly elegant, but gets the job done:
s = "2,5,7-9,12"
ret = []
for tok in s.split(","):
val = map(int, tok.split("-"))
if len(val) == 1:
ret += val
else:
ret += range(val[0], val[1] + 1)
print ret
One area where this solution may need work is the handling of negative numbers (it is not entirely clear from your question whether negative numbers can appear in the input).