Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder5.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'instance': 0.05; 'string,': 0.05; 'suppose': 0.05; 'ascii': 0.07; 'character,': 0.07; 'python': 0.07; 'from:addr:python': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'solves': 0.16; 'subject:Extracting': 0.16; 'expressions': 0.19; 'method.': 0.19; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'received:84': 0.25; 'string': 0.29; 'does': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'skip:" 10': 0.34; 'regular': 0.34; 'problem,': 0.35; 'difficult': 0.35; 'header:User-Agent:1': 0.35; 'reply-to:addr:python.org': 0.35; 'rather': 0.36; 'should': 0.37; 'to:addr:python.org': 0.39; 'could': 0.39; "it's": 0.40; 'here': 0.65; 'imagine': 0.72; 'reply-to:no real name:2**0': 0.72; 'header :Reply-To:1': 0.72; '--->': 0.84; 'alternative.': 0.84; 'candide': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjIHAH0mlU1UXebj/2dsb2JhbACYW4x/d4h5uBaFawSQbg Date: Fri, 01 Apr 2011 02:16:47 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Extracting subsequences composed of the same character References: <4d952008$0$3943$426a74cc@news.free.fr> In-Reply-To: <4d952008$0$3943$426a74cc@news.free.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301620676 news.xs4all.nl 34849 [::ffff:82.94.164.166]:54647 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2326 On 01/04/2011 01:43, candide wrote: > Suppose you have a string, for instance > > "pyyythhooonnn ---> ++++" > > and you search for the subquences composed of the same character, here > you get : > > 'yyy', 'hh', 'ooo', 'nnn', '---', '++++' > > It's not difficult to write a Python code that solves the problem, for > instance : > [snip] > > I should confess that this code is rather cumbersome so I was looking > for an alternative. I imagine that a regular expressions approach could > provide a better method. Does a such code exist ? Note that the string > is not restricted to the ascii charset. >>> import re >>> re.findall(r"((.)\2+)", s) [('yyy', 'y'), ('hh', 'h'), ('ooo', 'o'), ('nnn', 'n'), ('---', '-'), ('++++', '+')] >>> [m[0] for m in re.findall(r"((.)\2+)", s)] ['yyy', 'hh', 'ooo', 'nnn', '---', '++++']