Can python read up to where a certain pattern is matched?

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon Mar 8 23:51:59 EST 2004


On Tue, Mar 09, 2004 at 03:37:46PM +1100, Andrew Bennetts wrote:
> On Mon, Mar 08, 2004 at 08:11:16PM -0800, Josiah Carlson wrote:
> > >That, and I suspect that reading strings in chunks, rather than 1 byte at a
> > >time, would be much faster also.
> > 
> > Yes and no.  string.find(character) is fast.
> > 
> > seq = [read_chars.find(i) for i in break_characters]
> > min(filter(lambda inp:inp>-1, seq)+[-1])
> > 
> > May not be quite so fast for large numbers of break characters.
> 
> For finding any one of multiple characters, I expect the re module would
> work well, with the bonus that it could easily extend to coping with more
> complex patterns.

Or even just iterating the simple way -- assuming the generator's input is
chunks for strings, rather than single characters:

def split(chunks, ends=('.', '?', '!')):
    current_piece = ''
    for chunk in chunks:
        for char in chunk:
            current_piece += char
            if char in ends:
                yield current_piece
                current_piece = ''

(I know I'm concatenating strings the slow way again; it's just for clarity)

This has the disadvantage of not pushing any work into C code, but at least
it doesn't make any function calls either.

I still think the re module would be a better approach, of course, due to
being in C *and* not needing many function calls.

-Andrew.





More information about the Python-list mailing list