Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'fname': 0.09; 'iterate': 0.09; 'key)': 0.09; 'lst': 0.09; 'output,': 0.09; 'val,': 0.09; 'output': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'handle:': 0.16; 'iterating': 0.16; 'loud': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'simple.': 0.16; 'wrote:': 0.16; 'nested': 0.18; 'issue.': 0.20; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'error': 0.27; 'function': 0.28; 'question:': 0.29; 'code:': 0.29; 'print': 0.30; 'open': 0.33; 'file': 0.34; 'handle': 0.34; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'heard': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'desired': 0.37; 'why': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'address': 0.61; 'subject:skip:A 10': 0.63; 'latest': 0.64; 'below:': 0.71; "'for'": 0.84; 'dict()': 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=lzXDqSR0SeDz1p9sYIQA:9 a=okGF86O1iKPwMrsD:21 a=8wCFLB41R-EaT0fZ:21 a=QEXdDO2ut3YA: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> <103ea014-9bdb-4c37-acd1-cdc64f5f721e@googlegroups.com> From: MRAB Date: Wed, 12 Aug 2015 16:12:45 +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: <103ea014-9bdb-4c37-acd1-cdc64f5f721e@googlegroups.com> 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439392369 news.xs4all.nl 2861 [2001:888:2000:d::a6]:43111 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95282 On 2015-08-12 06:03, Ltc Hotspot wrote: > Message heard loud and clear: > > There are no error messages, the output is the issue. > > Question: What sorted function should I write to produce the desired > output, below: > Instead of iterating over "count.items()", iterate over "sorted(count.items())". > Desired output: > > 04 3 > 06 1 > 07 1 > 09 2 > 10 3 > 11 6 > 14 1 > 15 2 > 16 4 > 17 2 > 18 1 > 19 1 > > Latest revised code: > > count = dict() > fname = raw_input("Enter file name: ")# > handle = open (fname, 'r')# > for line in handle: > if line.startswith("From "): > address = line.split()[5] > line = line.rstrip() > count[address] = count.get(address, 0) + 1 > > lst = list() > for key,val in count.items(): > lst.append( (val, key) ) > lst.sort(reverse=True) > for val, key in lst[:12]: > print key,val > [snip] I don't know why you have a nested 'for' loop; just iterate over the sorted items and print them. Simple.