Search code examples
pythonpython-itertools

more efficient use of itertools.groupby()


I am trying to enhance my knowledge of the itertools library, since it's so generally useful. To that end, I'm trying to solve an interview puzzler I came across. A large portion of it involves sequentially counting the number of grouped and repeated digits within a number. For example, for the number:

1223444556

I want:

[(1,1),(2,2),(1,3),(3,4),(2,5),(1,6)]

which is to say, from left to right, there is 1 one, 2 twos, 1 three, and so forth.

Here is my current code:

from itertools import groupby
groups_first = [int(''.join(v)[0]) for k,v in groupby(str(1223444556))]
counts = [len(''.join(v)) for k,v in groupby(str(1223444556))]
zip(counts,groups_first)

It works, but what I would like to know is whether there is a more compact way of doing this that bypasses zipping two lists together. Any thoughts? I think this may go to doing some sort of lambda function in groupby(), but I can't see it yet.

Thanks!


Solution

  • How about:

    [(sum(1 for _ in v), int(k)) for k,v in groupby(str(1223444556))]