Compress a string

John Salerno johnjsal at NOSPAMgmail.com
Tue May 20 00:09:14 EDT 2008


On Sun, 18 May 2008 19:06:10 +0100
"Matt Porter" <mabbikeel at gmail.com> wrote:

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

Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.

def compress(s):
    new = [s[:1]]

    for c in s[1:]:
        if c not in new:
            new.append(c)
    return ''.join(new)



More information about the Python-list mailing list