Given a list:
>>> l = ['x', 'x', 'y', 'y', 'x']
I could get the count of the list by using collections.Counter
:
>>> from collections import Counter
>>> Counter(l)
Counter({'x': 3, 'y': 2})
How can I count contiguous items instead of the global count of the elements in the list? E.g.
>>> l = ['x', 'x', 'y', 'y', 'x']
>>> ContiguousCounter(l)
[('x',2), ('y',2), ('x', 1)]
>>> l = ['x', 'x', 'y', 'y', 'x', 'x', 'x', 'y']
>>> ContiguousCounter(l)
[('x',2), ('y',2), ('x', 3), ('y', 1)]