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

Josiah Carlson jcarlson at nospam.uci.edu
Sat Mar 6 13:57:27 EST 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:
         sentence.append(char)
         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