[Tutor] can this be done easerly

Roelof Wobben rwobben at hotmail.com
Mon Aug 30 11:44:06 CEST 2010


Hello,

 

For a exerise I made this one :"

 

import string

def extract_words(s):
    """
      >>> extract_words('Now is the time!  "Now", is the time? Yes, now.')
      ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
      >>> extract_words('she tried to curtsey as she spoke--fancy')
      ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
    """
    word= ""
    s=string.lower(s)
    for char in s :
        if ord(char) >=65 and ord(char) <= 122 or ord(char)==32 or ord(char)==45:
              word= word + char 
    word=string.split(word, "--")
    word=string.join(word, " ")
    word=word.replace ("  ", " ")
    word=string.split(word, " ")
    return word
        
if __name__ == '__main__':
    import doctest
    doctest.testmod()

 

But now I wonder if this can be done more easerly ?

 

Roelof

 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100830/85095252/attachment.html>


More information about the Tutor mailing list