Given a string like:
num_str = "1,2,6,10,11-15,36,4,45-48"
I'm looking to return a sorted list as follows:
num_list = [1,2,4,6,10,11,12,13,14,15,36,45,46,47,48]
Looking for a responsible and reusable solution as my one-liner below is painfully illegible:
list(sorted(sum(((list(range(*[int(j) k for k,j in enumerate(i.split('-'))])) if '-' in i else [int(i)]) for i in num_str.split(',')), [])))
There may be existing libraries that do this well and that's ok to list here too.
CodePudding user response:
num_str = "1,2,6,10,11-15,36,4,45-48"
def parse(num_str):
num = []
for part in num_str.split(','):
p1 = part.split('-')
if len(p1) == 1:
num.append(int(p1[0]))
else:
num.extend(list(range(int(p1[0]),int(p1[1]) 1)))
return num
print(parse(num_str))
Output:
[1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 36, 45, 46, 47, 48]
CodePudding user response:
Another attempt using a generator
:
def normalize(num_str, sep=',', sep_range='-'):
for k in num_str.split(sep):
if sep_range not in k:
yield int(k)
else:
_r1, _r2 = [int(j) for j in k.split(sep_range)]
yield from range(_r1, _r2 1)
num_str = "1,2,6,10,11-15,36,4,45-48"
out = list(sorted(normalize(num_str)))
print(out)
Output:
[1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 36, 45, 46, 47, 48]
CodePudding user response:
num_str = "1,2,6,10,11-15,36,4,45-48"
def function(string):
result = []
for i in string.split(','):
if i.isdigit():
result.append(int(i))
elif not i.isdigit() and '-' in i:
val1, val2 = map(int, i.split('-'))
result.extend(list(range(val1, val2 1)))
return result
print(function(num_str))
output
[1, 2, 6, 10, 11, 12, 13, 14, 15, 36, 4, 45, 46, 47, 48]
CodePudding user response:
What about this one?
>>> from itertools import groupby
>>> num_str = "1,2,6,10,11-15,36,4,45-48"
>>>
>>> lst = []
>>> for k, v in groupby(num_str.split(','), lambda x: '-' in x):
... if not k:
... lst.extend(int(i) for i in v)
... else:
... for low, high in [i.split('-') for i in v]:
... lst.extend(range(int(low), int(high) 1))
...
>>> lst
[1, 2, 6, 10, 11, 12, 13, 14, 15, 36, 4, 45, 46, 47, 48]
>>> sorted(lst)
[1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 36, 45, 46, 47, 48]
CodePudding user response:
From intspan, (need to install with pip install intspan
), this would look like this.
>>> from intspan import intspan
>>> num_str = "1,2,6,10,11-15,36,4,45-48"
>>> list(intspan(num_str))
[1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 36, 45, 46, 47, 48]
CodePudding user response:
For a one liner this does get a bit unwieldly/unreadable compared to readable methods in other answers:
list(sorted(sum(((list(range(*[int(j) k for k,j in enumerate(i.split('-'))])) if '-' in i else [int(i)]) for i in num_str.split(',')), [])))
Output:
[1, 2, 4, 6, 10, 11, 12, 13, 14, 15, 36, 45, 46, 47, 48]