Newbie question - better way to do this?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun May 27 19:03:47 EDT 2007


On Sun, 27 May 2007 14:55:42 -0700, John Machin wrote:

> 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)


Bzzzt! Somebody didn't read the Original Poster's comment "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".

Of course, for somebody whose requirements _aren't_ broken, you would be
completely right. Besides, I'm under no obligation to write all the O.P.'s
code for him, just point him in the right direction.


-- 
Steven.




More information about the Python-list mailing list