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
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
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
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
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
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
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