-
-
Save anonymous/e0f4b3cc3430e6d99112 to your computer and use it in GitHub Desktop.
Convert hyphen string to list of integers
This file contains 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 hyphen_range(s): | |
"""From hyphen string to list of integers""" | |
if not s: | |
return [] | |
result = [] | |
for x in s.split(','): | |
try: | |
result.append(int(x)) | |
except ValueError: | |
couple, step = x.split(':') | |
start, end = couple.split('-') | |
result.extend(range(int(start), int(end) + 1, int(step))) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment