Newbie question - better way to do this?

Eric venner at gmail.com
Sun May 27 09:44:01 EDT 2007


I have some working code, but I realized it is just the way I would
write it in C, which means there is probably a better (more pythonic)
way of doing it.

Here's the section of code:

    accumulate = firstIsCaps = False
    accumStart = i = 0
    while i < len(words):
        firstIsCaps = firstIsCapitalized(words[i])
        if firstIsCaps and not accumulate:
            accumStart, accumulate = i, True
        elif accumulate and not firstIsCaps:
            doSomething(words[accumStart : i])
            accumulate = False
        i += 1

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

Thanks,
Eric




More information about the Python-list mailing list