Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'collections': 0.09; 'splitting': 0.09; 'python': 0.10; 'jan': 0.11; 'wed,': 0.15; "'from": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'i.e.,': 0.16; 'pair.': 0.16; 'sorting': 0.16; 'wrote:': 0.16; 'string': 0.17; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'fairly': 0.22; 'am,': 0.23; 'seems': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'message-id:@mail.gmail.com': 0.27; 'finally,': 0.27; 'print': 0.30; 'getting': 0.33; 'sat': 0.33; 'case,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'subject:: ': 0.37; '12,': 0.37; 'skip:o 20': 0.38; 'entire': 0.61; 'subject:skip:A 10': 0.63; 'hour': 0.69; 'below:': 0.71; 'counts': 0.81; '10:43': 0.84; 'chrisa': 0.84; 'collection,': 0.84; 'hour,': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=0wb4zJbk/q05wQxkIG06Draj0Hq9+t63Qdt1HEeDQXs=; b=xFrvDB2tMkj1PIcoKpEWRQ1BVECQU3mQFLO1jKY4fzbajQ+EKMP8biS92nSHFbcTB/ NqRtQy1oMC06I6N39EoAYF11zU4xhew/kPPitadZXQGBu3jK+zF/xn/vQ3OD7VmqfzJf uFexTSZ82e9z67v5Im/ey/lE1CuZU8E7wBxUGwRvnUEdvwFoOdmMVWMrJBVB32KLreA5 0mHlcfjV7DCaoYXdOvvH5fRMOfpFV3rOgGgr4cyzgsFqyE6GdrtXNSeTq+4W3TYQB0pF dPY0cdWDJK/Fir/984gUirD0gmdJb1TPslKLyFYmz4EGF4V7uqHdD2uKqdk6LbRCd5Nk E2Ig== MIME-Version: 1.0 X-Received: by 10.50.109.233 with SMTP id hv9mr22045952igb.92.1439340545211; Tue, 11 Aug 2015 17:49:05 -0700 (PDT) In-Reply-To: References: <0baa3bd5-9f80-4d4e-9367-84e2a32d8c70@googlegroups.com> <55CA92BA.7070905@mrabarnett.plus.com> Date: Wed, 12 Aug 2015 10:49:05 +1000 Subject: Re: AttributeError From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439340553 news.xs4all.nl 2892 [2001:888:2000:d::a6]:34270 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95266 On Wed, Aug 12, 2015 at 10:43 AM, Ltc Hotspot wrote: > Python can pull the hour from the 'From ' line by finding the time and then > splitting the string a second time using a colon, i.e., From > stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 > > Finally, accumulated the counts for each hour, print out the counts, sorted > by hour as shown below: In that case, you want to sort the entire collection, not a single key-value pair. It seems to me you can do this fairly efficiently with collections.Counter. import collections with open(raw_input("Enter file name: ")) as f: counts = collections.Counter(line.split()[5].rstrip() for line in f if line.startswith("From ")) counts = counts.items() counts.sort() for hour, count in counts: print hour, count The most important part is getting items() and then sorting the whole thing. ChrisA