Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35097
| Date | 2012-12-19 12:21 +0100 |
|---|---|
| From | Thomas Bach <thbach@students.uni-mainz.de> |
| 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 |
| References | <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1040.1355916192.29569.python-list@python.org> (permalink) |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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