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 c not in

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 Try this t = set(bbc) list(t

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 an indentifier will shadow the builtin str

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(C) Traceback

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 elegant as the one- and two-liners, but somewhat concise I guess. def compress(s

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:

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 enumerate(str): try: if c != str[i

[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 succinct, but a solution is currently escaping me. def

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

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 so far feels like it could be made clearer

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 - ABC The code I have so far feels like it could be made clearer

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 is currently escaping me. def compress_str(str): new_str = for i, c in enumerate

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 = for i, c in enumerate(str): try

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 at two successive elements of a sequence: from itertools import izip instr

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' import re re.compile(r(.)\1*).sub(r\1, aaa) 'abc' Peter -- http://mail.python.org/mailman

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 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(aabbcc)) 'abc' import re re.compile(r(.)\1*).sub(r\1

Re: Compress a string

2008-05-18 Thread inhahe
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 inhahe
) return new_str Matt Porter [EMAIL PROTECTED] wrote in message 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