Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.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; 'mrab': 0.05; 'effect.': 0.09; 'fname': 0.09; 'items)': 0.09; 'splitting': 0.09; 'python': 0.10; 'jan': 0.11; 'everyone,': 0.15; "'from": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'handle:': 0.16; 'i.e.,': 0.16; 'key/value': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'order?': 0.16; 'pairs': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'sorting': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; '2015': 0.20; 'aug': 0.20; 'to:name:python-list@python.org': 0.20; 'keys': 0.22; 'file.': 0.22; 'trying': 0.22; 'code,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'header:User- Agent:1': 0.26; 'figure': 0.27; 'error': 0.27; 'equivalent': 0.27; 'order.': 0.27; 'raw': 0.27; 'finally,': 0.27; 'values': 0.28; 'hour.': 0.29; "i'm": 0.30; 'print': 0.30; 'url:mailman': 0.30; 'code': 0.30; '(i.e.,': 0.30; 'e.g.': 0.30; 'embedded': 0.32; 'received:84': 0.32; 'url:python': 0.33; 'extract': 0.33; 'sat': 0.33; 'open': 0.33; 'url:listinfo': 0.34; 'tue,': 0.34; 'file': 0.34; 'handle': 0.34; 'list': 0.34; "isn't": 0.35; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'desired': 0.37; 'doing': 0.38; 'why': 0.39; 'data': 0.39; 'received:192': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; "you'll": 0.61; 'address': 0.61; 'subject:skip:A 10': 0.63; 'more': 0.63; 'dat': 0.66; 'reverse': 0.66; 'url:tinyurl': 0.67; 'hour': 0.69; 'below:': 0.71; 'address,': 0.77; 'counts': 0.81; '12:': 0.84; 'dict()': 0.84; 'hour,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf 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=mNP-igo6AAAA:20 a=e17fi3lIAAAA:20 a=1QTY2m9xAAAA:20 a=8AHkEIZyAAAA:8 a=lykzQ68nZSM61WgJ62EA:9 a=VVBCYzpdopgaPmfw:21 a=XrZrP9DLv6fHq0D4:21 a=QEXdDO2ut3YA:10 a=WUqUqFxxm2YA:10 a=VXVrhB-izI0A:10 a=efQeCdOcnUIA:10 X-AUTH: mrabarnett@:2500 Subject: Re: AttributeError References: <0baa3bd5-9f80-4d4e-9367-84e2a32d8c70@googlegroups.com> <55CA92BA.7070905@mrabarnett.plus.com> From: MRAB To: "python-list@python.org" Date: Wed, 12 Aug 2015 01:58:20 +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: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439341104 news.xs4all.nl 2899 [2001:888:2000:d::a6]:42321 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95268 On 2015-08-12 01:43, Ltc Hotspot wrote: > The Assignment: > > > > I'm trying to write Python code to read through a data file and figure > out the distribution by hour of the dat for each message in the data file. > > 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: > > name = raw_input("Enter file:") > if len(name) < 1 : name = "mbox-short.txt" > handle = open(name) > > > 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 > Well, line.split()[5] isn't the address, it's the time, e.g. "09:14:16". You need to do just a little more work to extract the hour. I don't know what you think you'll achieve by sorting key/value pairs in reverse order. What you should be doing is sorting the keys (hours), although sorting the pairs of keys and values (i.e., the items) would have the same effect. Have a look at the 'sorted' function. > > > Raw data code, available at http://tinyurl.com/ob89r9p > Embedded data code, available at http://tinyurl.com/qhm4ppq > Visualization URL link, available at http://tinyurl.com/ozzmffy > > Regards, > Hal > > On Tue, Aug 11, 2015 at 5:26 PM, MRAB > wrote: > > On 2015-08-12 01:01, Ltc Hotspot wrote: > > Hi Everyone, > > > What is the list equivalent to line 12: ncount.sort(reverse=True) > > > 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 > > for key,val in count.items(): > ncount = (key,val) > ncount.sort(reverse=True) > print key,val > > > Error message, reads: AttributeError, line 12, below : 'tuple' > object has no attribute 'sort' > > Raw data code, available at http://tinyurl.com/ob89r9p > Embedded data code, available at http://tinyurl.com/qhm4ppq > Visualization URL link, available at http://tinyurl.com/ozzmffy > > What are you trying to do? Why are you trying to sort a key/value > pair in reverse order? > > -- > https://mail.python.org/mailman/listinfo/python-list > >