Coupla nits:
On Tue, 15 Feb 2005 14:39:30 -0500, Kent Johnson <[EMAIL PROTECTED]> 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 coun
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
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 flawless
> 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 val
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 have
to remove all punctuation (either with regexes or with multiple
foo.replace('[symbol]',
Thanks to everyone who replied to my post. All of your
suggestions seem to work. My thanks
Ron
--- Ryan Davis <[EMAIL PROTECTED]> wrote:
> You could use split() to split the contents of the
> file into a list of strings.
>
> ###
> >>> x = 'asdf foo bar foo'
> >>> x.split()
> ['asdf', 'foo', 'b
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 i
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 cou
You could use split() to split the contents of the file into a list of strings.
###
>>> x = 'asdf foo bar foo'
>>> x.split()
['asdf', 'foo', 'bar', 'foo']
###
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
Ron,
is 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
10 matches
Mail list logo