Compress a string

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Mon May 19 15:58:07 EDT 2008


inhahe wrote:

> i see lots of neat one-liner solutions but just for the sake of
> argument:
> 
> def compress_str(str):
>      new_str = ""
>      lc = ""
>      for c in str:
>         if c != lc: new_str.append(c)
>      return new_str

Please test before posting.

>>> compress_str("AAAABBBBCCCCC")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in compress_str
AttributeError: 'str' object has no attribute 'append'

Regards,


Björn

-- 
BOFH excuse #48:

bad ether in the cables




More information about the Python-list mailing list