Compress a string
Paul McGuire
ptmcg at austin.rr.com
Sun May 18 15:07:33 EDT 2008
On May 18, 1:45 pm, Gary Herron <gher... at islandtraining.com> wrote:
> Matt Porter wrote:
> > Hi guys,
>
> > I'm trying to compress a string.
> > E.g:
> > "AAAABBBC" -> "ABC"
>
I'm partial to using (i)zip when I need to look at two successive
elements of a sequence:
>>> from itertools import izip
>>> instr = "aaaaaaaaaaabbbbbbbbbbdddddaaaaaaaddddddccccccc"
>>> newstr = "".join( b for a,b in izip(" "+instr,instr) if a!=b )
>>> print newstr
abdadc
-- Paul
More information about the Python-list
mailing list