Compress a string
Matt Porter
mabbikeel at gmail.com
Sun May 18 17:31:20 EDT 2008
On Sun, 18 May 2008 20:30:57 +0100, Peter Otten <__peter__ at web.de> wrote:
> 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'
>
Brilliant - I was trying to figure out how to do this with the re
capabilities.
Thanks to everyone
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
--
More information about the Python-list
mailing list