Answers for "python how to count consecutive 1 in a string"

1

count consecutive values in python

#count consecutif 1 in list. list exemple l=['1','1','0','1','0','1,'1',1']
cpt=0
    compte=[]
    for i in ch:
        if i=='1':
            cpt +=1
        else:
            compte.append(cpt)
            cpt=0
    compte.append(cpt)
Posted by: Guest on February-28-2021
1

count consecutive values in python

>>> from itertools import groupby
>>> def groups(l):
...     return [sum(g) for i, g in groupby(l) if i == 1]
...
>>> groups([0,1,0,0,0])
[1]
>>> groups([0,0,1,1,0])
[2]
>>> groups([1,1,0,1,1])
[2, 2]
Posted by: Guest on February-28-2021

Code answers related to "python how to count consecutive 1 in a string"

Python Answers by Framework

Browse Popular Code Answers by Language