Compress a string

Peter Otten __peter__ at web.de
Sun May 18 15:30:57 EDT 2008


Matt Porter wrote:

> I'm trying to compress a string.
> E.g:
>   "AAAABBBC" -> "ABC"

Two more:

>>> from itertools import groupby
>>> "".join(k for k, g in groupby("aaaaaabbbbbbbbbbcccccc"))
'abc'

>>> import re
>>> re.compile(r"(.)\1*").sub(r"\1", "aaaaaaabbbbcccccccc")
'abc'

Peter



More information about the Python-list mailing list