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


Groups > comp.lang.python > #35105

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

Newsgroups comp.lang.python
Date 2012-12-19 03:37 -0800
References <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com> <mailman.1040.1355916192.29569.python-list@python.org>
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
From dgcosgrave@gmail.com
Message-ID <mailman.1043.1355919690.29569.python-list@python.org> (permalink)

Show all headers | View raw


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!

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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