Given a list
A = [1, 6, 13, 15, 17, 18, 19, 21, 29, 36, 53, 58, 59, 61, 63, 78, 79, 81, 102, 114]
is there an easy way to group all those clusters where the difference between successive elements is smaller than 3?
That is, obtain something like:
[13, 15, 17, 19, 21], [58, 59, 61, 63], [78, 79, 81]
I was wondering whether there exists any built-in function, but I couldn't find anything similar. I was trying to figure it out using the groupby
of the itertools
, but I'm stuck. Thank you in advance.