Skip to content

Instantly share code, notes, and snippets.

@gerryjenkinslb
Created March 10, 2018 20:18
Show Gist options
  • Save gerryjenkinslb/705b0715120a826c66107c7a62dc4245 to your computer and use it in GitHub Desktop.
Save gerryjenkinslb/705b0715120a826c66107c7a62dc4245 to your computer and use it in GitHub Desktop.
convert sequences in string simular to "1,4,8-12,14" to generator of sequence 1,4,8,9,10,11,12,14
# gen sequence i.e. "1,3,5-7,9" > [1,3,5,6,7,9]
def seq_gen(s):
for item in s.split(','):
if '-' in item:
a, b = map(int, item.split('-'))
for i in range(a,b+1):
yield i
else:
if len(item):
yield int(item)
print(list(seq_gen("1,3,5-7,9")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment