[Tutor] AttributeError,
Ltc Hotspot
ltc.hotspot at gmail.com
Wed Aug 12 06:53:03 CEST 2015
Hi Steven,
Message heard loud and clear:
Question: What sorted function should I write to produce the desired
output, below:
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
Output code:
In [3]: %run assignment_10_2_v_01
Enter file name: mbox-short.txt
16:23:48 1
16:23:48 1
11:11:52 1
17:07:00 1
16:23:48 1
11:11:52 1
17:07:00 1
16:23:48 1
11:11:52 1
04:07:34 1
17:07:00 1
16:23:48 1
11:11:52 1
07:02:32 1
04:07:34 1
17:07:00 1
16:23:48 1
11:12:37 1
11:11:52 1
07:02:32 1
04:07:34 1
17:07:00 1
16:23:48 1
14:50:18 1
11:12:37 1
11:11:52 1
07:02:32 1
04:07:34 1
17:07:00 1
16:23:48 1
14:50:18 1
11:35:08 1
11:12:37 1
11:11:52 1
07:02:32 1
04:07:34 1
17:07:00 1
16:23:48 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
07:02:32 1
04:07:34 1
18:10:48 1
17:07:00 1
16:23:48 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
07:02:32 1
04:07:34 1
18:10:48 1
17:07:00 1
16:23:48 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
11:10:22 1
07:02:32 1
04:07:34 1
19:51:21 1
18:10:48 1
17:07:00 1
16:23:48 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
11:10:22 1
07:02:32 1
04:07:34 1
19:51:21 1
18:10:48 1
17:07:00 1
16:23:48 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
11:10:22 1
07:02:32 1
19:51:21 1
18:10:48 1
17:07:00 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
11:10:22 1
19:51:21 1
18:10:48 1
17:07:00 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
11:10:22 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
11:11:52 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
14:50:18 1
11:37:30 1
11:35:08 1
11:12:37 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
11:35:08 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
11:35:08 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
11:35:08 1
19:51:21 1
18:10:48 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
11:35:08 1
19:51:21 1
18:10:48 1
17:18:23 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
19:51:21 1
18:10:48 1
17:18:23 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
19:51:21 1
18:10:48 1
17:18:23 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
19:51:21 1
18:10:48 1
17:18:23 1
17:07:00 1
16:34:40 1
16:29:07 1
16:23:48 1
16:10:39 1
15:46:24 1
15:03:18 1
14:50:18 1
11:37:30 1
In [4]:
Regards,
Hal
On Tue, Aug 11, 2015 at 8:32 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, Aug 11, 2015 at 07:38:21PM -0700, Ltc Hotspot wrote:
>> Steven,
>>
>> Visit the URL links below to view the latest revised code:
>
> I don't think so. I don't have access to the web right now, but I do
> have access to email. And even if I did, I'm lazy and wouldn't follow
> links and then have to copy and paste from the website into my reply.
>
> Since I'm donating my time for free, the least you can do is do the
> copying and pasting yourself.
>
>> Output: 09:14:16
>> Syntax message: val is not defined
>
> I'm pretty sure that is not the actual error message you get. Are you
> sure it is not a NameError, rather than SyntaxError?
>
> The kind of error you get, together with the error message, often gives
> you clues as to what is going on. Python goes to a huge amount of
> trouble to provide a useful and informative error message, instead of
> just saying "Error!" and leaving you to guess. So read the message: if
> it tells you that "val is not defined", then you have not defined a
> variable val.
>
>
> --
> Steve
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list