["python","numpy"]
How to find the groups of consecutive elements in a NumPy array

I have to cluster the consecutive elements from a NumPy array. Considering the following example a = [ 0, 47, 48, 49, 50, 97, 98, 99] The output should be a list of tuples as follows [(0), (47, 48, 49, 50), (97, 98, 99)] Here the difference is just one between the elements. It will be great if the difference can also be specified as a limit or a hardcoded number.
Here's a lil func that might help: def group_consecutives(vals, step=1): """Return list of consecutive lists of numbers from vals (number list).""" run = [] result = [run] expect = None for v in vals: if (v == expect) or (expect is None): run.append(v) else: run = [v] result.append(run) expect = v + step return result >>> group_consecutives(a) [[0], [47, 48, 49, 50], [97, 98, 99]] >>> group_consecutives(a, step=47) [[0, 47], [48], [49], [50, 97], [98], [99]] P.S. This is pure Python. For a NumPy solution, see unutbu's answer.