I have a list like
[38, 98, 110, 111, 112, 120, 121, 898]
How can I merge consecutive numbers into a pair representing a range?
desired output:
['38',
'98',
'110,112',
'120,121',
'898']
I have a list like
[38, 98, 110, 111, 112, 120, 121, 898]
How can I merge consecutive numbers into a pair representing a range?
desired output:
['38',
'98',
'110,112',
'120,121',
'898']
You can do this nicely with itertools.groupby by noticing that the difference between the items in your list, and a counter are constant while the numbers are consecutive
eg
38 - 0 = 38
98 - 1 = 97
110 - 2 = 108
111 - 3 = 108
112 - 4 = 108
120 - 5 = 115
121 - 6 = 115
898 - 7 = 891
in this example, the 108's are grouped together and the 115's are grouped together. Now some code
>>> from itertools import groupby, count
>>> L = [38, 98, 110, 111, 112, 120, 121, 898]
>>> groups = groupby(L, key=lambda item, c=count():item-next(c))
>>> tmp = [list(g) for k, g in groups]
see what we have so far
>>> tmp
[[38], [98], [110, 111, 112], [120, 121], [898]]
convert to the desired result
>>> [str(x[0]) if len(x) == 1 else "{},{}".format(x[0],x[-1]) for x in tmp]
['38', '98', '110,112', '120,121', '898']
A straight-forward solution:
def ranger(lst):
fr, to = lst[0], lst[0]
for x in lst[1:]:
if x == to+1:
to = x
else:
yield fr, to
fr, to = x, x
yield fr, to
res = [','.join(map(str, sorted(set(x)))) for x in ranger(lst)]