[Tutor] can this be done easerly

ALAN GAULD alan.gauld at btinternet.com
Mon Aug 30 20:56:14 CEST 2010


Ok, Looks like you need the replace() on anything that could be a separator...

BTW.
Did you see Steven's implementation? He used a list comprehension 
which is probably a little beyond your "comprehension" for now (sorry, 
I couldn't resist! :-) but can be translated easily back to an explicit loop. 


He is using your original character based approach but with isalpha() 
instead of the ord() checks

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>
>From: Roelof Wobben <rwobben at hotmail.com>
>To: alan.gauld at btinternet.com
>Sent: Monday, 30 August, 2010 18:41:58
>Subject: RE: [Tutor] can this be done easerly
>
> Hello, 
> 
>It don't work for me:
> 
>I changed everything to this :
> 
>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']
>    """
>    s2=[]
>    s = s.lower()
>    for word in s.split():
>        test = word.strip('!?,.--')
>        s2.append(test)
>    return s2
>        
>if __name__ == '__main__':
>    import doctest
>    doctest.testmod()
> 
>But now the doctest failes with this :
> 
>Expected:
>['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
>Got:
>['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke--fancy']
>**********************************************************************
>1 items had failures:
>1 of 2 in __main__.extract_words
***Test Failed*** 1 failures.
>
> 
>________________________________
 Date: Mon, 30 Aug 2010 17:01:04 +0000
>From: alan.gauld at btinternet.com
>Subject: Re: [Tutor] can this be done easerly
>To: rwobben at hotmail.com
>CC: tutor at python.org
>
> 
>
>
>> I tried your suggestion with strip and it looks like this :
> 
>> import string
>
>You don't need this now
>
>> def extract_words(s):
>>    word= ""
>
>You dont need this
>
>>     s2=[]
>>     s = string.lower(s)
>>     s = s.replace( "--", " ")
>
>You probably don't need this either - just add '-' to the strip set.
>
>>     s = s.split()
>>     for word in s:
>
>And you could combine those to
>
>for word in s.split():
>
>>         test = word 
>>         test = word.strip('!?,.')
>
>You don't need test, just use
>
>word = word.strip('!?",.-')  #added " and - to your set...
>
>>         s2.append(test)
>>     return s2
>        
>> It works well only I can't get it to work the strip with ". 
>> I always get a EOF error message. I tried it with 
>> word.strip('!?,.") and with word.strip('!?,./"'') but no go.
>
>It looks like you are mixing quote styles.
>Notice how I included the " in the strip() above... 
>See if that works for you.
>
>Alan G.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100830/942a70e0/attachment.html>


More information about the Tutor mailing list