Typing tutor help script needed, please

RedGrittyBrick RedGrittyBrick at SpamWeary.foo
Fri Oct 28 15:23:15 EDT 2005


<reinstated the context that was omitted by SPE>

 > Throw wrote:
 >> I must be able to extract words which contain only
 >> certain letters (they need not contain all of those letters,
 >> but they may not contain any other letters).

SPE - Stani's Python Editor wrote:
> #---Input data
> 
> #List of words; every word or sentence on one line
> #If from file: WORDS = open(fileName).readlines()
> WORDS = """\
> Afrikaans
> Anna
> Bread
> red
> word
> bored
> python""".split('\n')
> 
> #---Main program
> import re
> 
> PATTERN         = ['[^(%s)]+','[%s]+']
> FILENAME        = ['not_%s.txt','%s.txt']
> 
> def filter(letters='bdeor',words=WORDS,contain=True,ignoreCase=True):
>     pattern     = PATTERN[contain]%'|'.join(list(letters))
>     if ignoreCase:
>         allowed = re.compile(pattern,re.IGNORECASE)
>     else:
>         allowed = re.compile(pattern)
>     result      = []
>     for word in words:
>         match   = allowed.match(word)
>         if match and match.group(0) == word: result.append(word)
>     print result
>     output      = open(FILENAME[contain]%letters,'w')
>     output.write('\n'.join(result))
>     output.close()
> 
> 
> 
> if __name__ == '__main__':
>     filter()
> 
> ---------------
> This should do it.
> 

Am I underestimating the task, it looks simple enough for the simplest grep?

# perl -n -e "print if /^[bdeor]+$/i" words.txt
red
bored



More information about the Python-list mailing list