Parse string of integer sets with intervals to list?

S = "2,5,7-9,12" ranges = (x. Split("-") for x in s. Split(",")) print I for r in ranges for I in range(int(r0), int(r-1) + 1) prints 2, 5, 7, 8, 9, 12.

Really nice. We even can combine it in one line. – disfated Apr 18 at 15:29.

S = "2,5,7-9,12" result = list() for item in s. Split(','): if '-' in item: x,y = item. Split('-') result.

Extend(range(int(x), int(y)+1)) else: result. Append(int(item)) print result.

As far as I can tell, this is correct. – Sven Marnach Apr 18 at 15:15.

Not that I'm aware of, but you can easily make your own: Create a results list. Split strings by , and start iterating over the result. If the current string contains a - append a range to the list.

If the current string is a number, append it to the list. Else return an error. Return the list.

Someone must be in a bad mood to downvote all those correct answers. I'll bring them back to zero :) – Sven Marnach Apr 18 at 15:19.

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(val0, val1 + 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).

2 This seems to be correct as well. Why the downvote? – Sven Marnach Apr 18 at 15:17 In my case I only have to handle positive int's, although it is good to have universal solution.

– disfated Apr 18 at 15:35.

I would define function: def make_range(s): out = s = s. Split(',') for n in s: if '-' in n: n = n. Split('-') for I in range(int(n0), int(n1) + 1): out.

Append(i) else: out. Append(int(n)) return out print make_range("2,5,7-9,12") #output 2, 5, 7, 8, 9, 12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions