Path: csiph.com!eternal-september.org!feeder.eternal-september.org!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweak.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed7.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'collections': 0.09; 'nameerror:': 0.09; 'splitting': 0.09; 'python': 0.10; 'jan': 0.11; 'wed,': 0.15; "'from": 0.16; 'chris,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'i.e.,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'pair.': 0.16; 'received:192.168.1.4': 0.16; 'sorting': 0.16; 'wrote:': 0.16; 'string': 0.17; '2015': 0.20; 'aug': 0.20; 'fairly': 0.22; 'am,': 0.23; 'code,': 0.23; 'defined': 0.23; 'seems': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'finally,': 0.27; 'module.': 0.27; "skip:' 10": 0.28; 'print': 0.30; 'code': 0.30; 'getting': 0.33; 'sat': 0.33; 'case,': 0.34; 'tue,': 0.34; 'file': 0.34; 'execution': 0.35; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'skip:o 20': 0.38; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'entire': 0.61; 'subject:skip:A 10': 0.63; 'url:tinyurl': 0.67; 'hour': 0.69; 'below:': 0.71; 'counts': 0.81; '10:43': 0.84; 'collection,': 0.84; 'hour,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=UWU4mQFGWXkA:10 a=IkcTkHD0fZMA:10 a=VrYZ2vDwAAAA:20 a=pGLkceISAAAA:8 a=py8ZigT-v36dcxcEyYAA:9 a=QEXdDO2ut3YA:10 a=15hvf8TFd6oA:10 X-AUTH: mrabarnett@:2500 Subject: Re: AttributeError To: python-list@python.org References: <0baa3bd5-9f80-4d4e-9367-84e2a32d8c70@googlegroups.com> <55CA92BA.7070905@mrabarnett.plus.com> From: MRAB Date: Wed, 12 Aug 2015 16:09:06 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439392156 news.xs4all.nl 2960 [2001:888:2000:d::a6]:40302 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95281 On 2015-08-12 04:05, Ltc Hotspot wrote: > Chris, > > Check the code and the visualize execution of the code, available at > http://tinyurl.com/p8tgd5h > > message reads: NameError: name 'collections' is not defined > You didn't import the module. > > Regards, > Hal > > On Tue, Aug 11, 2015 at 5:49 PM, Chris Angelico > wrote: > > 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. >