Newbie question - better way to do this?

John Machin sjmachin at lexicon.net
Sun May 27 17:55:42 EDT 2007


On May 28, 12:46 am, Steven D'Aprano
<s... at REMOVE.THIS.cybersource.com.au> wrote:
> 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 = []
>
Bzzzt. Needs the following code at the end:
if accumulator:
    doSomething(accumulator)




More information about the Python-list mailing list