[Tutor] can this be done easerly
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...
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
Author of the Learn To Program website
>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
>It don't work for me:
>I changed everything to this :
> >>> 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']
> s = s.lower()
> for word in s.split():
> test = word.strip('!?,.--')
> return s2
>if __name__ == '__main__':
> import doctest
>But now the doctest failes with this :
>['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
>['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
>> 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...
>> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor