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.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 To: 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: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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.