I have an array ar = [2,2,2,1,1,2,2,3,3,3,3]. For this array, I want to find the lengths of consecutive same numbers like:
ar = [2,2,2,1,1,2,2,3,3,3,3]
values: 2, 1, 2, 3 l
You can do this with groupby
In [60]: from itertools import groupby In [61]: ar = [2,2,2,1,1,2,2,3,3,3,3] In [62]: print [(k, sum(1 for i in g)) for k,g in groupby(ar)] [(2, 3), (1, 2), (2, 2), (3, 4)]