3 ms·
import itertools [(c, len(list(it))) for (c, it) in itertools.groupby("aaaabbbcca")] It's not the first time I've wanted def ilen(it): return sum(1 fo
by eesmith 6y ago
import itertools
[(c, len(list(it))) for (c, it) in itertools.groupby("aaaabbbcca")]
It's not the first time I've wanted
def ilen(it):
return sum(1 for _ in it)
as part of itertools. Which would make the second line be:
[(c, itertools.ilen(it)) for (c, it) in itertools.groupby("aaaabbbcca")]
Rejected many times, eg, from 2010: https://bugs.python.org/issue9547 https://bugs.python.org/issue9547 .