I have a list like this
[0,0,1,1,1,0,0,0,1,1,0,0,1,1,1,1]
and I want to group them and then find the length of each group. So the result will be like that:
[[2,0],[3,1].....[4,1]]
Use itertools.groupby
:
>>> import itertools
>>> l = [0,0,1,1,1,0,0,0,1,1,0,0,1,1,1,1]
>>> [(len(list(g)), k) for k,g in itertools.groupby(l)]
[(2, 0), (3, 1), (3, 0), (2, 1), (2, 0), (4, 1)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With