Newbie question - better way to do this?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sun May 27 10:30:50 EDT 2007
En Sun, 27 May 2007 10:44:01 -0300, Eric <venner at gmail.com> escribió:
> 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).
Using groupby:
py> from itertools import groupby
py>
py> words = "Este es un Ejemplo. Los Ejemplos usualmente son tontos. Yo
siempre
escribo tonterias.".split()
py>
py> for upper, group in groupby(words, str.istitle):
... if upper:
... print list(group) # doSomething(list(group))
...
['Este']
['Ejemplo.', 'Los', 'Ejemplos']
['Yo']
You could replace your firstIsCapitalized function instead of the string
method istitle(), but I think it's the same. See
http://docs.python.org/lib/itertools-functions.html
--
Gabriel Genellina
More information about the Python-list
mailing list