Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file (http://www.velocityreviews.com/forums/t955641-counting-how-often-the-same-word-appears-in-a-txt-file-but-my-codeonly-prints-the-last-line-entry-in-the-txt-file.html)

dgcosgrave@gmail.com 12-19-2012 10:45 AM

counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file
 
Hi Iam just starting out with python...My code below changes the txt file into a list and add them to an empty dictionary and print how often the word occurs, but it only seems to recognise and print the last entry of the txt file. Any help would be great.

tm =open('ask.txt', 'r')
dict = {}
for line in tm:
line = line.strip()
line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
line = line.lower()
list = line.split(' ')
for word in list:
if word in dict:
count = dict[word]
count += 1
dict[word] = count
else:
dict[word] = 1
for word, count in dict.iteritems():
print word + ":" + str(count)

Jussi Piitulainen 12-19-2012 10:55 AM

Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
 
dgcosgrave@gmail.com writes:

> Hi Iam just starting out with python...My code below changes the txt
> file into a list and add them to an empty dictionary and print how
> often the word occurs, but it only seems to recognise and print the
> last entry of the txt file. Any help would be great.
>
> tm =open('ask.txt', 'r')
> dict = {}
> for line in tm:
> line = line.strip()
> line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
> line = line.lower()
> list = line.split(' ')
> for word in list:
> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count
> else:
> dict[word] = 1
> for word, count in dict.iteritems():
> print word + ":" + str(count)


The "else" clause is mis-indented (rather, mis-unindented).

Python's "for" statement does have an optional "else" clause. That's
why you don't get a syntax error. The "else" clause is used after the
loop finishes normally. That's why it catches the last word.

Steven D'Aprano 12-19-2012 11:03 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:

> Hi Iam just starting out with python...My code below changes the txt
> file into a list and add them to an empty dictionary and print how often
> the word occurs, but it only seems to recognise and print the last entry
> of the txt file. Any help would be great.
>
> tm =open('ask.txt', 'r')
> dict = {}
> for line in tm:
> line = line.strip()
> line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
> line = line.lower()
> list = line.split(' ')


Note: you should use descriptive names. Since this is a list of WORDS, a
much better name would be "words" rather than list. Also, list is a built-
in function, and you may run into trouble when you accidentally re-use
that as a name. Same with using "dict" as you do.

Apart from that, so far so good. For each line, you generate a list of
words. But that's when it goes wrong, because you don't do anything with
the list of words! The next block of code is *outside* the for-loop, so
it only runs once the for-loop is done. So it only sees the last list of
words.

> for word in list:


The problem here is that you lost the indentation. You need to indent the
"for word in list" (better: "for word in words") so that it starts level
with the line above it.

> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count


This bit is fine.

> else:
> dict[word] = 1


But this fails for the same reason! You have lost the indentation.

A little-known fact: Python for-loops take an "else" block too! It's a
badly named statement, but sometimes useful. You can write:


for value in values:
do_something_with(value)
if condition:
break # skip to the end of the for...else
else:
print "We never reached the break statement"

So by pure accident, you lined up the "else" statement with the for loop,
instead of what you needed:

for line in tm:
... blah blah blah
for word in words:
if word in word_counts: # better name than "dict"
... blah blah blah
else:
...


> for word, count in dict.iteritems():
> print word + ":" + str(count)


And this bit is okay too.


Good luck!


--
Steven

Thomas Bach 12-19-2012 11:21 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
Hi,

just as a side-note

On Wed, Dec 19, 2012 at 02:45:13AM -0800, dgcosgrave@gmail.com wrote:
> for word in list:
> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count
> else:
> dict[word] = 1


When you got the indentation and names right, you can restate this as

import collections
counter = collections.Counter(words)

in Python 2.7 or as

import collections
counter = collections.defaultdict(int)
for word in words:
counter[word] += 1

in Python 2.6

Regards,
Thomas.

dgcosgrave@gmail.com 12-19-2012 11:28 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Wednesday, December 19, 2012 11:55:28 PM UTC+13, Jussi Piitulainen wrote:
>
>
>
>
> > Hi Iam just starting out with python...My code below changes the txt

>
> > file into a list and add them to an empty dictionary and print how

>
> > often the word occurs, but it only seems to recognise and print the

>
> > last entry of the txt file. Any help would be great.

>
> >

>
> > tm =open('ask.txt', 'r')

>
> > dict = {}

>
> > for line in tm:

>
> > line = line.strip()

>
> > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

>
> > line = line.lower()

>
> > list = line.split(' ')

>
> > for word in list:

>
> > if word in dict:

>
> > count = dict[word]

>
> > count += 1

>
> > dict[word] = count

>
> > else:

>
> > dict[word] = 1

>
> > for word, count in dict.iteritems():

>
> > print word + ":" + str(count)

>
>
>
> The "else" clause is mis-indented (rather, mis-unindented).
>
>
>
> Python's "for" statement does have an optional "else" clause. That's
>
> why you don't get a syntax error. The "else" clause is used after the
>
> loop finishes normally. That's why it catches the last word.


Thanks for quick reply Jussi...indentation fixed the problem :-)

dgcosgrave@gmail.com 12-19-2012 11:34 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Thursday, December 20, 2012 12:03:21 AM UTC+13, Steven D'Aprano wrote:
> On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:
>
>
>
> > Hi Iam just starting out with python...My code below changes the txt

>
> > file into a list and add them to an empty dictionary and print how often

>
> > the word occurs, but it only seems to recognise and print the last entry

>
> > of the txt file. Any help would be great.

>
> >

>
> > tm =open('ask.txt', 'r')

>
> > dict = {}

>
> > for line in tm:

>
> > line = line.strip()

>
> > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

>
> > line = line.lower()

>
> > list = line.split(' ')

>
>
>
> Note: you should use descriptive names. Since this is a list of WORDS, a
>
> much better name would be "words" rather than list. Also, list is a built-
>
> in function, and you may run into trouble when you accidentally re-use
>
> that as a name. Same with using "dict" as you do.
>
>
>
> Apart from that, so far so good. For each line, you generate a list of
>
> words. But that's when it goes wrong, because you don't do anything with
>
> the list of words! The next block of code is *outside* the for-loop, so
>
> it only runs once the for-loop is done. So it only sees the last list of
>
> words.
>
>
>
> > for word in list:

>
>
>
> The problem here is that you lost the indentation. You need to indent the
>
> "for word in list" (better: "for word in words") so that it starts level
>
> with the line above it.
>
>
>
> > if word in dict:

>
> > count = dict[word]

>
> > count += 1

>
> > dict[word] = count

>
>
>
> This bit is fine.
>
>
>
> > else:

>
> > dict[word] = 1

>
>
>
> But this fails for the same reason! You have lost the indentation.
>
>
>
> A little-known fact: Python for-loops take an "else" block too! It's a
>
> badly named statement, but sometimes useful. You can write:
>
>
>
>
>
> for value in values:
>
> do_something_with(value)
>
> if condition:
>
> break # skip to the end of the for...else
>
> else:
>
> print "We never reached the break statement"
>
>
>
> So by pure accident, you lined up the "else" statement with the for loop,
>
> instead of what you needed:
>
>
>
> for line in tm:
>
> ... blah blah blah
>
> for word in words:
>
> if word in word_counts: # better name than "dict"
>
> ... blah blah blah
>
> else:
>
> ...
>
>
>
>
>
> > for word, count in dict.iteritems():

>
> > print word + ":" + str(count)

>
>
>
> And this bit is okay too.
>
>
>
>
>
> Good luck!
>
>
>
>
>
> --
>
> Steven


Thanks Steven appreciate great info for future coding. i have change names to be more decriptive and corrected the indentation... all works! cheers

dgcosgrave@gmail.com 12-19-2012 11:37 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote:
> Hi,
>
>
>
> just as a side-note
>
>
>
> On Wed, Dec 19, 2012 at 02:45:13AM -0800, :
>
> > for word in list:

>
> > if word in dict:

>
> > count = dict[word]

>
> > count += 1

>
> > dict[word] = count

>
> > else:

>
> > dict[word] = 1

>
>
>
> When you got the indentation and names right, you can restate this as
>
>
>
> import collections
>
> counter = collections.Counter(words)
>
>
>
> in Python 2.7 or as
>
>
>
> import collections
>
> counter = collections.defaultdict(int)
>
> for word in words:
>
> counter[word] += 1
>
>
>
> in Python 2.6
>
>
>
> Regards,
>
> Thomas.


Thanks Thomas for your time... using 2.7 great!

dgcosgrave@gmail.com 12-19-2012 11:37 AM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote:
> Hi,
>
>
>
> just as a side-note
>
>
>
> On Wed, Dec 19, 2012 at 02:45:13AM -0800, :
>
> > for word in list:

>
> > if word in dict:

>
> > count = dict[word]

>
> > count += 1

>
> > dict[word] = count

>
> > else:

>
> > dict[word] = 1

>
>
>
> When you got the indentation and names right, you can restate this as
>
>
>
> import collections
>
> counter = collections.Counter(words)
>
>
>
> in Python 2.7 or as
>
>
>
> import collections
>
> counter = collections.defaultdict(int)
>
> for word in words:
>
> counter[word] += 1
>
>
>
> in Python 2.6
>
>
>
> Regards,
>
> Thomas.


Thanks Thomas for your time... using 2.7 great!

Dennis Lee Bieber 12-19-2012 06:29 PM

Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file
 
On Wed, 19 Dec 2012 02:45:13 -0800 (PST), dgcosgrave@gmail.com declaimed
the following in gmane.comp.python.general:


> tm =open('ask.txt', 'r')
> dict = {}
> for line in tm:
> line = line.strip()
> line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
> line = line.lower()
> list = line.split(' ')


You could (though it gets a bit long) combine some of the above...

list = line.strip().translate(
None,
"the punctuation set"
).lower().split()

# taking advantage that open ()/[]/{} automatically continue on next
lines

> for word in list:


INDENTATION! As coded, you first do the strip/translate/lower/split
on EACH line of the file... THEN you are processing the words in the
LAST line processed in the previous loop.

> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count
> else:
> dict[word] = 1


More indentation -- I suspect your want the else: and following line
to be indented the same as the if line...

Though the whole block can be simplified to

dict[word] = dict.get(word, 0) + 1

> for word, count in dict.iteritems():
> print word + ":" + str(count)

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/



All times are GMT. The time now is 03:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.