[Tutor] regexp
Steven D'Aprano
steve at pearwood.info
Sat Nov 5 00:00:55 CET 2011
Dinara Vakhitova wrote:
> Hello,
>
> I need to find the words in a corpus, which letters are in the alphabetical
> order ("almost", "my" etc.)
Quoting Jamie Zawinski:
Some people, when confronted with a problem, think "I know, I'll
use regular expressions." Now they have two problems.
Now you have two problems: find words in the corpus which are in
alphabetical order, and get the damn regular expression to work correctly.
Don't use a regex for this. It is much simpler to write a Python
function to solve it:
def letters_in_order(astring):
"""Return True if letters in astring are in alphabetical order.
>>> letters_in_order("almost")
True
>>> letters_in_order("zoology")
False
"""
if len(astring) <= 1:
return True
for i in range(1, len(astring)):
if astring[i] < astring[i-1]:
# Pair of characters are out of order.
return False
# If none of the pairs are out of order, they whole string
# must be in order.
return True
words = filter(letters_in_order, corpus)
for word in words:
print(word)
--
Steven
More information about the Tutor
mailing list