Re: Compress a string
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 new] return ''.join(new) Wow, list comprehensions are cool. And it's a misuse of list comprehension here IMHO because they are meant to build lists, not to cram a ``for``/``if`` loop into a one liner. You are building a list full of `None` objects, just to throw it away. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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) Won't keep the ordering. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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 type. new_str = for i, c in enumerate(str): try: if c != str[i+1]: new_str += c except IndexError: new_str += c return new_str Now everyone gave smart solutions, may I suggest the stupidier possible one: def compress_string(astring): compressed = [] for c in astring: if c not in compressed: compressed.append(c) return ''.join(compressed) Not elegant, but at least very clear. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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 (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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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): new = [s[:1]] for c in s[1:]: if c not in new: new.append(c) return ''.join(new) -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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: 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 new] return ''.join(new) Wow, list comprehensions are cool. -- http://mail.python.org/mailman/listinfo/python-list
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 compress_str(str): new_str = for i, c in enumerate(str): try: if c != str[i+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt -- -- -- http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED]: Re: Compress a string]
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 compress_str(str): new_str = for i, c in enumerate(str): try: if c != str[i+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt def compress_str(s): # str is a builtin keyword. Don't overload it. out = [] for c in s: if out and c == out[-1]: out.append(c) # This is faster than new_str += c return ''.join(out) -- http://mail.python.org/mailman/listinfo/python-list
Fwd: Re: [EMAIL PROTECTED]: Re: Compress a string]
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]
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 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+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt def compress_str(s): # str is a builtin keyword. Don't overload it. out = [] for c in s: if out and c == out[-1]: out.append(c) # This is faster than new_str += c return ''.join(out) Thanks. Had to change a few bits to make it behave as I expected: def compress_str(s): # str is a builtin keyword. Don't overload it. out = [s[0],] for c in s: if out and c != out[-1]: out.append(c) # This is faster than new_str += c return ''.join(out) Feels slightly less hacked together -- http://mail.python.org/mailman/listinfo/python-list -- -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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 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+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt -- -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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(str): try: if c != str[i+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt -- -- string = 'sspppaaam' ''.join(x for x, y in zip(string, '\0'+string) if x != y) 'spam' HTH PS: I keep seeing problems on this list whose solution seems to involve 'window' iterating over a sequence. E.g. list(window('eggs', 2)) [('e', 'g'), ('g', 'g'), ('g', 's')] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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: if c != str[i+1]: new_str += c except IndexError: new_str += c return new_str Cheers Matt This is shorter and perhaps clearer: def compress_str(str): new_str = for c in str: if not new_str or c != new_str[-1]: new_str += c return new_str However, successive appends to a string is inefficient (whereas successive appends to a list are ok), so this is probably more efficient: def compress_str(str): r = [] for c in str: if not r or c != r[-1]: r.append(c) # Build a list of characters return ''.join(r)# Join list into a single string And then building a list in a loop is usually more efficient (and perhaps clearer) if done with a list comprehension: new_str = ''.join([c for i,c in enumerate(str) if not i or str[i-1] != c]) or, maybe clearer as two lines: r = [c for i,c in enumerate(str) if not i or str[i-1] != c] new_str = ''.join(r) Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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 = aaabbdaaaddccc newstr = .join( b for a,b in izip( +instr,instr) if a!=b ) print newstr abdadc -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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/listinfo/python-list
Re: Compress a string
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
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, aaa) 'abc' Brilliant - I was trying to figure out how to do this with the re capabilities. Thanks to everyone Peter -- http://mail.python.org/mailman/listinfo/python-list -- -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
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 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 Matt -- -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Compress a string
I forgot a line that says, lc = c i should really test my stuff. inhahe [EMAIL PROTECTED] wrote in message news:... 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 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 Matt -- -- -- http://mail.python.org/mailman/listinfo/python-list