count consecutive values in python

Solutions on MaxInterview for count consecutive values in python by the best coders in the world

showing results for - "count consecutive values in python"
Felix
06 Apr 2018
1#count consecutif 1 in list. list exemple l=['1','1','0','1','0','1,'1',1']
2cpt=0
3    compte=[]
4    for i in ch:
5        if i=='1':
6            cpt +=1
7        else:
8            compte.append(cpt)
9            cpt=0
10    compte.append(cpt)
Francesca
13 Jul 2020
1>>> from itertools import groupby
2>>> def groups(l):
3...     return [sum(g) for i, g in groupby(l) if i == 1]
4...
5>>> groups([0,1,0,0,0])
6[1]
7>>> groups([0,0,1,1,0])
8[2]
9>>> groups([1,1,0,1,1])
10[2, 2]