Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.szaf.org!feedme.news.telefonica.de!telefonica.de!diesel.cu.mi.it!.POSTED!not-for-mail From: giacomo boffi Newsgroups: comp.lang.python Subject: Re: Python solve problem with string operation Date: Fri, 17 Jan 2014 01:17:12 +0100 Organization: The Sun and the Rain. Lines: 38 Message-ID: <87eh47a2d3.fsf@pascolo.net> References: NNTP-Posting-Host: ppp-151-0.21-151.libero.it Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: virtdiesel.mng.cu.mi.it 1389917833 7124 151.21.0.151 (17 Jan 2014 00:17:13 GMT) X-Complaints-To: abuse@diesel.cu.mi.it NNTP-Posting-Date: Fri, 17 Jan 2014 00:17:13 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.5-b34 (linux) Cancel-Lock: sha1:8zrBpBOGhiPYKhgqWqenuLcHJEg= Xref: csiph.com comp.lang.python:64117 Nac Temha writes: > 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. % cat a.py def f(s,n): if s[n+1] == s[n]: return s[:n]+s[n+1:], n return s, n+1 i = "344111133311222223377" n = 0 while n+1 != len(i): i, n = f(i, n) print i % python a.py 34131237 % -- your instructor is a mean person