Created
March 10, 2018 20:18
-
-
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
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
# 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