The aim is to find groups of increasing/monotonic numbers given a list of integers. Each item in the resulting group must be of a +1 increment from the previous item
I think this works. It's not fancy but it's simple. It constructs a start list sl
and an end list el
, which should always be the same length, then uses them to index into x
:
def igroups(x):
sl = [i for i in range(len(x)-1)
if (x == 0 or x[i] != x[i-1]+1) and x[i+1] == x[i]+1]
el = [i for i in range(1, len(x))
if x[i] == x[i-1]+1 and (i == len(x)-1 or x[i+1] != x[i]+1)]
return [x[sl[i]:el[i]+1] for i in range(len(sl))]