Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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]
 
PREVIOUS NEXT
Tagged: #count #consecutive #values #python
ADD COMMENT
Topic
Name
1+6 =