[Tutor] count words

Bill Mill bill.mill at gmail.com
Tue Feb 15 21:02:30 CET 2005

Coupla nits:

On Tue, 15 Feb 2005 14:39:30 -0500, Kent Johnson <kent37 at tds.net> wrote:
> from string import punctuation
> from time import time
> words = open(r'D:\Personal\Tutor\ArtOfWar.txt').read().split()

Another advantage of the first method is that it allows a more elegant
word counting algorithm if you choose not to read the entire file into
memory. It's a better general practice to consume lines from a file
via the "for line in f" idiom.

> words = [ word.strip(punctuation) for word in words ]

And, be careful with this - punctuation does not include whitespace
characters. Although that is no problem in this example, because
split() strips its component strings automatically, people should be
aware that punctuation won't work on strings that haven't had their
whitespace stripped.

Otherwise though, good stuff.

Bill Mill
bill.mill at gmail.com

More information about the Tutor mailing list