> Subject: Re: [Tutor] can this be done easerly > From: evert....@gmail.com > Date: Mon, 30 Aug 2010 12:04:08 +0200 > CC: tutor@python.org > To: rwob...@hotmail.com > > > 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 ? > > Using regular expressions could work, depending on your view of regular > expressions being 'easy': > > import re > re.split('\W+', s.lower()) > > will do most of what you want (though you'll end up with the occasional empty > string. > > Evert > Hello Evert, Thank you for the answer. I following this tutorial (http://openbookproject.net/thinkcs/python/english2e/) and till chapter 10 there is no talking about regular expressions. So this is not easy for me. But thanks , I will read on regular expressions so I understand that one too. Roelof
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor