Newbie question - better way to do this?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun May 27 10:46:46 EDT 2007


On Sun, 27 May 2007 06:44:01 -0700, Eric wrote:

> words is a big long array of strings.  What I want to do is find
> consecutive sequences of words that have the first letter capitalized,
> and then call doSomething on them.  (And you can ignore the fact that
> it won't find a sequence at the very end of words, that is fine for my
> purposes).

Assuming the list of words will fit into memory, and you can probably
expect to fit anything up to millions of words comfortably into memory,
something like this might be suitable:

list_of_words = "lots of words go here".split()

accumulator = []
for word in list_of_words:
    if word.istitle():
        accumulator.append(word)
    else:
        doSomething(accumulator)
        accumulator = []


-- 
Steven.




More information about the Python-list mailing list