Python solve problem with string operation

Rhodri James rhodri at wildebst.org.uk
Thu Jan 16 20:05:41 EST 2014


On Thu, 16 Jan 2014 22:24:40 -0000, Nac Temha <naccttemha at gmail.com> wrote:

> Hi everyone,
>
> I want to do operation with chars in the given string. Actually I want to
> grouping the same chars.
>
> For example;
>
> input : "344111133311222223377"
> operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
> output: "34131237"
>
>
>
> How can I do without list, regular expression. just using string
> operations. Using an effective methods of python for this problem.

I almost convinced myself this was homework, you know.  A hint as to why  
you might want such a thing would look a lot less suspicious :-)

The simplest way to do this is probably using groupby:


 from itertools import groupby

input = "344111133311222223377"
output = "".join(k for k, _ in groupby(s))
print output



-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list