Skip to content

Instantly share code, notes, and snippets.

/hypen_range.py Secret

Created March 26, 2012 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e0f4b3cc3430e6d99112 to your computer and use it in GitHub Desktop.
Save anonymous/e0f4b3cc3430e6d99112 to your computer and use it in GitHub Desktop.
Convert hyphen string to list of integers
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