Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95268
| Subject | Re: AttributeError |
|---|---|
| References | <0baa3bd5-9f80-4d4e-9367-84e2a32d8c70@googlegroups.com> <55CA92BA.7070905@mrabarnett.plus.com> <CALa1VB7f1-4Jrp+FCXEY5oCES+epXzMLLNatB_uiiZ2X7=8pPw@mail.gmail.com> |
| From | MRAB <python@mrabarnett.plus.com> |
| Date | 2015-08-12 01:58 +0100 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.94.1439341104.3627.python-list@python.org> (permalink) |
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 <mailto: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 <python@mrabarnett.plus.com
> <mailto:python@mrabarnett.plus.com>> 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
>
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 17:01 -0700
Re: AttributeError leo kirotawa <kirotawa@gmail.com> - 2015-08-11 21:16 -0300
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:26 +0100
Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 10:49 +1000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:58 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 22:03 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:12 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 15:50 +0000
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:29 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 17:45 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:57 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 18:24 +0100
RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 10:31 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 10:37 -0700
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 11:35 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 19:59 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 12:05 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:38 +0000
RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 14:15 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 14:32 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:45 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:51 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 14:04 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-13 10:37 +0100
Re: AttributeError Ben Finney <ben+python@benfinney.id.au> - 2015-08-13 21:08 +1000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 21:04 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 21:16 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 23:35 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:05 -0700
Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 16:15 -0700
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:16 +0100
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:46 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 09:15 +0000
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-13 02:41 -0700
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 11:06 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:56 +0100
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:28 +0000
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 21:58 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 03:46 +0100
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 04:32 +0100
Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 13:49 +1000
Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 11:36 +0000
Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:09 +0100
Re: AttributeError Gene Heskett <gheskett@wdtv.com> - 2015-08-12 17:25 -0400
Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 15:02 -0700
Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 23:16 +0100
csiph-web