[Tutor] Finding the "streaks" in heads/tails list

Kent Johnson kent37 at tds.net
Thu Oct 2 01:01:04 CEST 2008


On Wed, Oct 1, 2008 at 6:00 PM, Danny Yoo <dyoo at cs.wpi.edu> wrote:
>> Regular expressions are for processing strings, not loops.
>
> From a theoretical point of view, this isn't quite true: regular
> expressions can deal with sequences of things.

Sheesh! OK, *Python* regular expressions are for processing strings :-)

> If we want to be cute, we can also use the itertools.groupby()
> function to do the clumping of identical sequential values for us.
> For example:
>
> #################################################
>>>> for group in itertools.groupby('aaaabbbbcaaabaaaacc'):
> ...     print group[0], len(list(group[1]))

Hmm, I smell a one-liner here:
max(len(list(group[1])) for group in itertools.groupby('aaaabbbbcaaabaaaacc'))

Kent


More information about the Tutor mailing list