Given: a list, such as l=[4,4,4,4,5,5,5,6,7,7,7] Todo: get the count of an element and keep their occurrence order, e.g.: [(4,4),(5,3),(6,1),(7,3)]
I could do it wit
Use groupby:
groupby
>>> l = [4,4,4,4,5,5,5,6,7,7,7,2,2] >>> from itertools import groupby >>> [(i, l.count(i)) for i,_ in groupby(l)] [(4, 4), (5, 3), (6, 1), (7, 3), (2, 2)]