Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35148

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

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject 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
Date 2012-12-19 13:29 -0500
Organization > Bestiaria Support Staff <
References <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1069.1355941724.29569.python-list@python.org> (permalink)

Show all headers | View raw


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/

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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 - 2012-12-19 02:45 -0800
  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 Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-12-19 12:55 +0200
    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 - 2012-12-19 03:28 -0800
  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 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 11:03 +0000
    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 - 2012-12-19 03:34 -0800
  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 Thomas Bach <thbach@students.uni-mainz.de> - 2012-12-19 12:21 +0100
    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 - 2012-12-19 03:37 -0800
    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 - 2012-12-19 03:37 -0800
  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 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-19 13:29 -0500

csiph-web