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
--
-- 12 1873
Try this
t = set("aaaaaaaaaaaabbbbbbbbbbccccccccc")
list(t)
Regards
Salvatore
"Matt Porter" <ma*******@gmail.coma écrit dans le message de news: ma***************************************@python.o rg...
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
--
--
"Matt Porter" <ma*******@gmail.comwrites:
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
--
--
>>string = 'sssssssssspppppppaaaaaaam' ''.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
Matt Porter wrote:
I'm trying to compress a string.
E.g:
"AAAABBBC" -"ABC"
Two more:
>>from itertools import groupby "".join(k for k, g in groupby("aaaaaabbbbbbbbbbcccccc"))
'abc'
>>import re re.compile(r"(.)\1*").sub(r"\1", "aaaaaaabbbbcccccccc")
'abc'
Peter
Matt Porter <ma*******@gmail.comwrote:
Hi guys,
I'm trying to compress a string.
E.g:
"AAAABBBC" -"ABC"
You mean like this?
>>''.join(c for c, _ in itertools.groupby("AAAABBBCAADCASS"))
'ABCADCAS'
HTH
Marc
On Sun, 18 May 2008 20:30:57 +0100, Peter Otten <__*******@web.dewrote:
Matt Porter wrote:
>I'm trying to compress a string. E.g: "AAAABBBC" -"ABC"
Two more:
>>>from itertools import groupby "".join(k for k, g in groupby("aaaaaabbbbbbbbbbcccccc"))
'abc'
>>>import re re.compile(r"(.)\1*").sub(r"\1", "aaaaaaabbbbcccccccc")
'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
--
--
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" <ma*******@gmail.comwrote in message
news:ma***************************************@pyt hon.org...
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.
Cheers
Matt
--
--
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
On Sun, 18 May 2008 19:06:10 +0100
"Matt Porter" <ma*******@gmail.comwrote:
Hi guys,
I'm trying to compress a string.
E.g:
"AAAABBBC" -"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)
On Tue, 20 May 2008 00:09:14 -0400
John Salerno <jo******@NOSPAMgmail.comwrote:
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.
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
Salvatore DI DI0 a écrit :
(top-post corrected - Salvatore, please, don't top-post)
"Matt Porter" <ma*******@gmail.coma écrit dans le message de news: ma***************************************@python.o rg...
>Hi guys,
I'm trying to compress a string. E.g: "AAAABBBC" -"ABC"
Try this
t = set("aaaaaaaaaaaabbbbbbbbbbccccccccc")
list(t)
Won't keep the ordering.
Matt Porter a écrit :
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):
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Champika Nirosh |
last post by:
Hi,
I have two machine where I needed to have a extended TCP/IP protocol to make
the link between the two machines
Mean,I need to write a application that compress every data the machine send...
|
by: Adriano |
last post by:
Can anyone recommend a simple way to compress/decomress a String in .NET 1.1 ?
I have a random string of 70 characters, the output from a DES3 encryption, and I wish to reduce the lengh of it,
...
|
by: vampire1986 |
last post by:
Hi all. I have project compress file using C#. I'm using name space IO.Compression and i saw this code on Internet but it is compress to file bigger than source file. Can you help me, please.Thanks...
|
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 ==...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |