Re: [Tutor] count words

2005-02-15 Thread Bill Mill
Ron, snipis there a way to do it so that I get a individual count of each word: word1 xxx word2 xxx words xxx etc. Ron, I'm gonna throw some untested code at you. Let me know if you understand it or not: word_counts = {} for line in f: for word in line.split(): if word in

Re: [Tutor] count words

2005-02-15 Thread Jeremy Jones
Ron Nixon wrote: I know that you can do this to get a count of home many times a word appears in a file f = open('text.txt').read() print f.count('word') Other than using a several print statments to look for seperate words like this, is there a way to do it so that I get a individual

Re: [Tutor] count words

2005-02-15 Thread Danny Yoo
On Tue, 15 Feb 2005, Ron Nixon wrote: I know that you can do this to get a count of home many times a word appears in a file f = open('text.txt').read() print f.count('word') Other than using a several print statments to look for seperate words like this, is there a way to do it so

Re: [Tutor] count words

2005-02-15 Thread Alan Gauld
Other than using a several print statments to look for seperate words like this, is there a way to do it so that I get a individual count of each word: word1 xxx word2 xxx words xxx The classic approach is to create a dictionary. Add each word as you come to it and increment the value by

Re: [Tutor] count words

2005-02-15 Thread Bill Mill
On Tue, 15 Feb 2005 18:03:57 +, Max Noel [EMAIL PROTECTED] wrote: On Feb 15, 2005, at 17:19, Ron Nixon wrote: Thanks to everyone who replied to my post. All of your suggestions seem to work. My thanks Ron Watch out, though, for all of this to work flawlessly you first

Re: [Tutor] count words

2005-02-15 Thread Kent Johnson
Ryan Davis wrote: Here's one way to iterate over that to get the counts. I'm sure there are dozens. ### x = 'asdf foo bar foo' counts = {} for word in x.split(): ... counts[word] = x.count(word) ... counts {'foo': 2, 'bar': 1, 'asdf': 1} ### The dictionary takes care of duplicates. If you are

Re: [Tutor] count words

2005-02-15 Thread Bill Mill
Coupla nits: On Tue, 15 Feb 2005 14:39:30 -0500, Kent Johnson [EMAIL PROTECTED] wrote: from string import punctuation from time import time snip words = open(r'D:\Personal\Tutor\ArtOfWar.txt').read().split() Another advantage of the first method is that it allows a more elegant word