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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:code': 0.07; 'subject:file': 0.07; 'python': 0.09; 'collections': 0.09; 'subject:same': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; '2.7': 0.13; 'dec': 0.15; 'great!': 0.16; 'subject: \n ': 0.16; 'subject:txt': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'import': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; '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; 'thursday,': 0.30; 'december': 0.32; 'hi,': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85': 0.35; 'received:209': 0.37; 'subject:: ': 0.38; 'your': 0.60; 'from:no real name:2**0': 0.60; 'thomas': 0.62; 'subject:...': 0.63; '20,': 0.65 Newsgroups: comp.lang.python Date: Wed, 19 Dec 2012 03:37:25 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.61.156.48; posting-account=GBJ5twoAAAAUtk007IrGiSpVrG2pLeLK References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 122.61.156.48 MIME-Version: 1.0 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 To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355919690 news.xs4all.nl 6886 [2001:888:2000:d::a6]:40368 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35105 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!