Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2327

Re: Extracting subsequences composed of the same character

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Extracting subsequences composed of the same character
Date 2011-03-31 21:40 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7F3220.21403831032011@news.panix.com> (permalink)
References <4d952008$0$3943$426a74cc@news.free.fr>

Show all headers | View raw


In article <4d952008$0$3943$426a74cc@news.free.fr>,
 candide <candide@free.invalid> 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', '---', '++++'

I got the following. It's O(n) (with the minor exception that the string 
addition isn't, but that's trivial to fix, and in practice, the bunches 
are short enough it hardly matters).

#!/usr/bin/env python                                                                               

s = "pyyythhooonnn ---> ++++"
answer = ['yyy', 'hh', 'ooo', 'nnn', '---', '++++']

last = None
bunches = []
bunch = ''
for c in s:
    if c == last:
        bunch += c
    else:
        if bunch:
            bunches.append(bunch)
        bunch = c
        last = c
bunches.append(bunch)

multiples = [bunch for bunch in bunches if len(bunch) > 1]
print multiples
assert(multiples == answer)


[eagerly awaiting a PEP for collections.bunch and 
collections.frozenbunch]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extracting subsequences composed of the same character candide <candide@free.invalid> - 2011-04-01 02:43 +0200
  Re: Extracting subsequences composed of the same character MRAB <python@mrabarnett.plus.com> - 2011-04-01 02:16 +0100
  Re: Extracting subsequences composed of the same character Roy Smith <roy@panix.com> - 2011-03-31 21:40 -0400
  Re: Extracting subsequences composed of the same character Tim Chase <python.list@tim.thechases.com> - 2011-03-31 20:58 -0500
  Re: Extracting subsequences composed of the same character Tim Chase <python.list@tim.thechases.com> - 2011-03-31 21:20 -0500
  Re: Extracting subsequences composed of the same character Terry Reedy <tjreedy@udel.edu> - 2011-04-01 00:18 -0400
  Re: Extracting subsequences composed of the same character candide <candide@free.invalid> - 2011-04-01 21:39 +0200

csiph-web