Reputation: 39
I have a sorted list like this [1,2,3,4,6,7,8,9,10,12,14]
I have looked up different similar solutions but they dont provide help in my case
I want this list to output like this
[ [1,4], [6,10], [12], [14] ]
so basically a list of lists with a start and an end of a sequence.
Honestly looks very easy but i am kind of stuck on it right now. Any help would be greatly appreciated !
Upvotes: 3
Views: 1872
Reputation: 676
Using numpy
import numpy as np
myarray = [1,2,3,4,6,7,8,9,10,12,14]
sequences = np.split(myarray, np.array(np.where(np.diff(myarray) > 1)[0]) + 1)
l = []
for s in sequences:
if len(s) > 1:
l.append((np.min(s), np.max(s)))
else:
l.append(s[0])
print(l)
Output:
[(1, 4), (6, 10), 12, 14]
Upvotes: 1
Reputation: 266
Solution can look like this
def make_ranges(l: list):
prev = l[0]
start = l[1]
res = []
for v in l[1:]:
if v - 1 != prev:
if start == prev:
res.append([start])
else:
res.append([start, prev])
start = v
prev = v
if l[-1] - 1 == l[-2]:
res.append([start, l[-1])
return res
For example:
print(make_ranges(list(range(10)) + list(range(13, 20)) + [22]))
This code will print [[0, 9], [13, 19], [22]]
Upvotes: 1
Reputation: 59274
Using pandas
import pandas as pd
s = pd.Series([1,2,3,4,6,7,8,9,10,12,14])
s.groupby(s.diff().ne(1).cumsum()).apply(lambda x: [x.iloc[0], x.iloc[-1]] if len(x) >= 2 else [x.iloc[0]]).tolist()
Outputs
[[1, 4], [6, 10], [12], [14]]
Upvotes: 1
Reputation: 26039
Use groupby
from the standard itertools
:
from itertools import groupby
lst = [1,2,3,4,6,7,8,9,10,12,14]
result = []
for k, g in groupby(enumerate(lst), lambda x: x[0] - x[1]):
g = list(map(lambda x: x[1], g))
if len(g) > 1:
result.append([g[0], g[-1]])
else:
result.append([g[0]])
print(result)
# [[1, 4], [6, 10], [12], [14]]
Upvotes: 1
Reputation: 20500
You can use more_itertools.consecutive_groups for this available at https://pypi.org/project/more-itertools/
from more_itertools import consecutive_groups
#Get the groupings of consecutive items
li = [list(item) for item in consecutive_groups([1,2,3,4,6,7,8,9,10,12,14])]
#[[1, 2, 3, 4], [6, 7, 8, 9, 10], [12], [14]]
#Use the result to get range groupings
result = [ [item[0],item[-1]] if len(item) > 1 else [item[0]] for item in li]
print(result)
#[[1, 4], [6, 10], [12], [14]]
Upvotes: 8