Re: Compress a string

2008-05-20 Thread Bruno Desthuilliers
Matt Porter a écrit : Hi guys, I'm trying to compress a string. E.g: "BBBC" -> "ABC" The code I have so far feels like it could be made clearer and more succinct, but a solution is currently escaping me. def compress_str(str): using 'str' as

Re: Compress a string

2008-05-20 Thread Bruno Desthuilliers
Salvatore DI DI0 a écrit : (top-post corrected - Salvatore, please, don't top-post) "Matt Porter" <[EMAIL PROTECTED]> a écrit dans le message de news: [EMAIL PROTECTED] Hi guys, I'm trying to compress a string. E.g: "BBBC" -> "ABC" > T

Re: Compress a string

2008-05-20 Thread Marc 'BlackJack' Rintsch
On Tue, 20 May 2008 00:38:57 -0400, John Salerno wrote: > def compress(s): > new = [] > > for c in s: > if c not in new: > new.append(c) > return ''.join(new) > > > No, wait! I can do better! > > def compress(s): > new = [] > [new.append(c) for c in s if

Re: Compress a string

2008-05-19 Thread John Salerno
On Tue, 20 May 2008 00:09:14 -0400 John Salerno <[EMAIL PROTECTED]> wrote: > 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. Whoops! Could be cleaner! :) def compress(s): new = [] for c in s:

Re: Compress a string

2008-05-19 Thread John Salerno
On Sun, 18 May 2008 19:06:10 +0100 "Matt Porter" <[EMAIL PROTECTED]> wrote: > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" Not that you need help anymore, but I decided to give it a try. Not as elegan

Re: Compress a string

2008-05-19 Thread Bjoern Schliessmann
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("

Re: Compress a string

2008-05-18 Thread inhahe
lc = "" > for c in str: >if c != lc: new_str.append(c) > return new_str > > > "Matt Porter" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Hi guys, >> >> I'm trying to compress a string. >

Re: Compress a string

2008-05-18 Thread inhahe
ge news:[EMAIL PROTECTED] > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" > > The code I have so far feels like it could be made clearer and more > succinct, but a solution is currently escaping me. > > > > > Cheers > Matt > -- > -- > -- http://mail.python.org/mailman/listinfo/python-list

Re: Compress a string

2008-05-18 Thread Matt Porter
On Sun, 18 May 2008 20:30:57 +0100, Peter Otten <[EMAIL PROTECTED]> wrote: Matt Porter wrote: I'm trying to compress a string. E.g: "BBBC" -> "ABC" Two more: from itertools import groupby "".join(k for k, g in groupby("aa

Re: Compress a string

2008-05-18 Thread Marc Christiansen
Matt Porter <[EMAIL PROTECTED]> wrote: > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" You mean like this? >>> ''.join(c for c, _ in itertools.groupby("BBBCAADCASS")) 'ABCADCAS' HTH Marc -- http://mail.python.org/mailman/listinfo/python-list

Re: Compress a string

2008-05-18 Thread Peter Otten
Matt Porter wrote: > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" Two more: >>> from itertools import groupby >>> "".join(k for k, g in groupby("aabbcc")) 'abc' >>&

Re: Compress a string

2008-05-18 Thread Paul McGuire
On May 18, 1:45 pm, Gary Herron <[EMAIL PROTECTED]> wrote: > Matt Porter wrote: > > Hi guys, > > > I'm trying to compress a string. > > E.g: > >  "BBBC" -> "ABC" > I'm partial to using (i)zip when I need to look a

Re: Compress a string

2008-05-18 Thread Gary Herron
Matt Porter wrote: Hi guys, I'm trying to compress a string. E.g: "BBBC" -> "ABC" The code I have so far feels like it could be made clearer and more succinct, but a solution is currently escaping me. def compress_str(str): new_str = "" f

Re: Compress a string

2008-05-18 Thread Arnaud Delobelle
"Matt Porter" <[EMAIL PROTECTED]> writes: > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" > > The code I have so far feels like it could be made clearer and more > succinct, but a solution

Re: Compress a string

2008-05-18 Thread Salvatore DI DI0
Try this t = set("bbc") list(t) Regards Salvatore "Matt Porter" <[EMAIL PROTECTED]> a écrit dans le message de news: [EMAIL PROTECTED] > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -&

Re: [EMAIL PROTECTED]: Re: Compress a string]

2008-05-18 Thread Matt Porter
On Sun, 18 May 2008 19:13:57 +0100, J. Clifford Dyer <[EMAIL PROTECTED]> wrote: On Sun, May 18, 2008 at 07:06:10PM +0100, Matt Porter wrote regarding Compress a string: Hi guys, I'm trying to compress a string. E.g: "BBBC" -> "ABC" The code I have

Fwd: Re: [EMAIL PROTECTED]: Re: Compress a string]

2008-05-18 Thread python
> I'm trying to compress a string. > E.g: > > "BBBC" -> "ABC" Doesn't preserve order, but insures uniqueness: line = "BBBC" print ''.join( set( line ) ) Malcolm -- http://mail.python.org/mailman/listinfo/python-list

[EMAIL PROTECTED]: Re: Compress a string]

2008-05-18 Thread J. Clifford Dyer
On Sun, May 18, 2008 at 07:06:10PM +0100, Matt Porter wrote regarding Compress a string: > > Hi guys, > > I'm trying to compress a string. > E.g: > "BBBC" -> "ABC" > > The code I have so far feels like it could be made clearer and more

Compress a string

2008-05-18 Thread Matt Porter
Hi guys, I'm trying to compress a string. E.g: "BBBC" -> "ABC" The code I have so far feels like it could be made clearer and more succinct, but a solution is currently escaping me. def compress_str(str): new_str = "" for i, c in enumera