Dictionary

Chris Angelico rosuav at gmail.com
Wed Jan 8 19:21:43 EST 2014


On Thu, Jan 9, 2014 at 11:13 AM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
>         #generate search re expression representing
>         #       .*              any character/multiple -- leading
>         #       [l|e|t|t|e|r]   match any of the letters supplied
>         #       .*              any character/multiple -- trailing
>         needle = ".*[" + "|".join(list(letters.lower())) + "].*"

I don't think this will do what you think it will. It'll match
anything that has any one of the supplied letters (or a pipe; it's a
character class, so the pipe has no significance and is simply part of
the class). I'm not sure a regex is the best thing here; but what you
could do is sort the letters and sort the letters in the words:

pat = ".*".join(sorted(letters.lower()))
for word in open("/usr/share/dict/words"): # Ought to use with
    if re.search(pat, ''.join(sorted(word))): print(word)

ChrisA



More information about the Python-list mailing list