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

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

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 val

Re: [Tutor] count words

2005-02-15 Thread Max Noel
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]',

RE: [Tutor] count words

2005-02-15 Thread Ron Nixon
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

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 i

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 cou

RE: [Tutor] count words

2005-02-15 Thread Ryan Davis
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

Re: [Tutor] count words

2005-02-15 Thread Bill Mill
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