473,289 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Re: [jcd@sdf.lonestar.org: Re: Compress a string]

On Sun, 18 May 2008 19:13:57 +0100, J. Clifford Dyer
<jc*@sdf.lonestar.orgwrote:
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:
"AAAABBBC" -"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


--
--

Jun 27 '08 #1
0 731

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: José Carlos | last post by:
Hi. How i could compress string of data?. I heart that it´s possible to make with librep, but i´dont know the way to do it. if somebody know any way to do it or any web where explain it i...
2
by: Senthil | last post by:
I have an XML in ASP which has to be compressed and sent to the browser client. I want to use a VB dll to do the job, is it possible?
5
by: anthonyberet | last post by:
I am an abject newbie, so mock away (actually no-one ever does that in this group..) Anyway, I want to replace one character in a string, based in that character's position in the string. For...
0
by: Christophe Elek | last post by:
Ok, I am completly at lost :) in both cases (my Yenc and zip utilities) i try to do the following 1) get a String 2) transform in byte 3) so some computation (either adding value to the byte...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
9
by: Plissken.s | last post by:
Hi, how can i compare a string which is non null and empty? i look thru the string methods here, but cant find one which does it? ...
0
by: J. Clifford Dyer | last post by:
On Sun, May 18, 2008 at 07:06:10PM +0100, Matt Porter wrote regarding Compress a string: def compress_str(s): # str is a builtin keyword. Don't overload it. out = for c in s: if out and c ==...
2
by: korean_dave | last post by:
How can i use the find() function on a string that is composed of tons of symbols that cause errors... THis is my string: find("<html><head><meta name="qrichtext" content="1" /><style...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.