Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

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]
Comment

PREVIOUS NEXT
Code Example
Python :: pandas swapaxes example 
Python :: django-sslserver 
Python :: how to use setattr Python 
Python :: open excel through python 
Python :: use python dotenv 
Python :: matplotlib vertical tick labels 
Python :: how to convert python input to int 
Python :: how to add percentage in countplot 
Python :: assert python 
Python :: notna pandas 
Python :: shell script to run python 
Python :: python cache 
Python :: buttons on canvas tkinter 
Python :: How to take space-separated integer input in Python 3 
Python :: print hexadecimal in python 
Python :: remove in list python 
Python :: python import graphviz 
Python :: python defaultdict(list) 
Python :: pandas count values by column 
Python :: closing a file in python 
Python :: pandas -inf and inf to 0 
Python :: how to get any letter of a string python 
Python :: coding planets 
Python :: django url patterns static 
Python :: Create a single executable from a Python project 
Python :: generate all combinatinosrs of a list pyton 
Python :: log loss python 
Python :: python how to find circumference of a circle 
Python :: remove special characters from string in python 
Python :: memory usage in python 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =