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


Groups > comp.lang.python > #35097

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

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <thbach@students.uni-mainz.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:code': 0.07; 'subject:file': 0.07; 'python': 0.09; 'collections': 0.09; 'subject:same': 0.09; '2.7': 0.13; 'dec': 0.15; 'subject: \n ': 0.16; 'subject:txt': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '2.6': 0.27; 'list:': 0.27; 'indentation': 0.29; 'subject:last': 0.30; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'charset:us-ascii': 0.36; 'subject:: ': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'content- disposition:inline': 0.60; 'subject:...': 0.63; 'email addr:gmail.com': 0.63; 'received:10.94': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1355916191; x=1387452191; h=date:from:to:subject:message-id:references:mime-version: in-reply-to; bh=h4l/FnefcOPxKPZJhAbdVSSSnA9EOh8YD1gwqfK8kKI=; b=Cz/hOtIkGiP0bHbhGPl919z9qWuMlZEOInKa88RLwmSBvyBxVpA2OGtu lxqQ6c018D6MlqhXLT/H0H7TDBenWrE1NQjETPQ2VbmGSED1zaj+wwf/e SrzBL4MZyKMTtz4G8rzZwKVHIa65Kj4BGz+ZOqBSIF5SHxPMdR5hFreHe w=;
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AoAGAPSh0VAKXgZY/2dsb2JhbABFg0i6UHOCHgEBBTpPCxgeBQsUDRwgE4gBAxOuXQ2JVYtiaYEagkhhA5Q0gVUBizeFEYJ1giE
Date Wed, 19 Dec 2012 12:21:57 +0100
From Thomas Bach <thbach@students.uni-mainz.de>
To <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
References <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Disposition inline
In-Reply-To <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com>
User-Agent Mutt/1.5.21 (2010-09-15)
X-Originating-IP [88.68.250.225]
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1040.1355916192.29569.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1355916192 news.xs4all.nl 6921 [2001:888:2000:d::a6]:40726
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35097

Show key headers only | View raw


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