Skip to content

Instantly share code, notes, and snippets.

@codebynumbers
Created August 8, 2013 00:41
Show Gist options
  • Select an option

  • Save codebynumbers/6180407 to your computer and use it in GitHub Desktop.

Select an option

Save codebynumbers/6180407 to your computer and use it in GitHub Desktop.
Convert a string like '0,1,2,10 - 12, 20-23' into '0,1,2,10,11,12,20,21,22,23'
def conv(ts):
results = Set([])
parts = ts.split(",")
for part in parts:
if "-" not in part:
results.add(int(part.strip()))
else:
(s, e) = part.split("-")
for i in range(int(s.strip()), int(e.strip()) + 1):
results.add(i)
return ",".join([str(x) for x in sorted(results)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment