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

Josiah Carlson jcarlson at
Sat Mar 6 19:57:27 CET 2004

> Note that I've only lightly tested that code, and it's probably very
> inefficient.

It is all right except for the string addition.  Each addition produces 
a new string.  For a final string of length n, you will have created n 
strings, and had to write n*(n-1)/2 = O(n^2) bytes. Ick.

For long sentences (more than 1000 characters, the timing difference 
becomes significant), the below is far faster:

def sentences(iterable):
     sentence = []
     for char in iterable:
         if char in ('.', '!', '?'):
             yield ''.join(sentence).strip()
             sentence = []
     sentence = ''.join(sentence).strip()
     if sentence:
         yield sentence

  - Josiah

More information about the Python-list mailing list