Created
March 6, 2019 14:32
-
-
Save dbadrian/1dab7e388dde2d1bffb5f3e372b02fec to your computer and use it in GitHub Desktop.
parse range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_range(astr): | |
""" Takes a string like "1-3,5,7-9" and returns a list [1,2,3,5,7,8,9] """ | |
result = set() | |
for part in astr.split(','): | |
x = part.split('-') | |
result.update(range(int(x[0]), int(x[-1]) + 1)) | |
return sorted(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment