Discussion:
[Tutor] AttributeError,
Ltc Hotspot
2015-08-11 23:24:39 UTC
Permalink
Hi Everyone,

Why is there an AttributeError, line 12, below : 'tuple' object has no
attribute 'sort'?

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



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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-12 00:39:37 UTC
Permalink
Post by Ltc Hotspot
Why is there an AttributeError, line 12, below : 'tuple' object has no
attribute 'sort'?
Because a tuple has no attribute sort.
A tuple is immutable - you can't change it. Therefore,
you can't sort it.

You can however use the sorted() function on it to
return a list containing the sorted contents of
your tuple.
Post by Ltc Hotspot
count = dict()
fname = raw_input("Enter file name: ")#
handle = open (fname, 'r')#
address = line.split()[5]
line = line.rstrip()
count[address] = count.get(address, 0) + 1
ncount = (key,val)
ncount.sort(reverse=True)
use

ncount = sorted(ncount, reverse=True)
Post by Ltc Hotspot
print key,val
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-12 00:45:08 UTC
Permalink
Post by Ltc Hotspot
Why is there an AttributeError, line 12, below : 'tuple' object has no
attribute 'sort'?
Having answered that in my last email I just
noticed another problem...
Post by Ltc Hotspot
count = dict()
fname = raw_input("Enter file name: ")#
handle = open (fname, 'r')#
address = line.split()[5]
line = line.rstrip()
count[address] = count.get(address, 0) + 1
ncount = (key,val)
ncount.sort(reverse=True)
You are trying to sort a tuple that contains
a single key,value pair. The key is an IP address
represented as a string and the value is an integer.
How do you expect those two values to sort?
Hint: Try it at the >>> prompt.
Post by Ltc Hotspot
print key,val
And having attempted to sort them you do nothing
with the result.

What are you trying to do with the sort?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-12 02:01:45 UTC
Permalink
Post by Ltc Hotspot
Hi Everyone,
Why is there an AttributeError, line 12, below : 'tuple' object has no
attribute 'sort'?
Haven't I see this exact same question, complete with solutions, on the
python-list mailing list?

The answer found there is that you are trying to sort the wrong value.
You are trying to sort an immutable (that is, unchangeable) (key, value)
tuple, which includes one string and one number. And then you ignore the
sorted result!

You have:

ncount = (key,val)
ncount.sort(reverse=True)
print key,val


Sorting (key, val) cannot work, because that is an immutable tuple.
Turning it into a list [key, val] now makes it sortable, but that
doesn't do what you want: Python 2 always sorts ints ahead of strings,
regardless of their actual values. But even if you did meaningfully sort
the list [key, val], having done so you don't look at the results, but
print the key and val variables instead, which are unchanged.

Changing the order of items in a list does not, and can not, change the
variables that were used to build that list.

If that is not clear, study this example:

py> a = 999
py> b = 1
py> alist = [a, b] # construct a list from a and b
py> print alist
[999, 1]
py> alist.sort() # change the order of items in the list
py> print alist
[1, 999]
py> print a, b # have a and b changed?
999 1


The actual solution needed is, I think, sorting the entire collection:

items = sorted(count.items())
for key, val in items:
print key,val
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-08-12 02:49:37 UTC
Permalink
Post by Steven D'Aprano
Post by Ltc Hotspot
Hi Everyone,
Why is there an AttributeError, line 12, below : 'tuple' object has no
attribute 'sort'?
Haven't I see this exact same question, complete with solutions, on the
python-list mailing list?
You have indeed. The answer from Chris Angelico was to use
collections.Counter, which is exactly what I told him here several days
ago. He's also put the same question on the core mentorship list just
for good measure.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-12 03:32:50 UTC
Permalink
Steven,
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ltc Hotspot
2015-08-12 04:53:03 UTC
Permalink
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
Post by Steven D'Aprano
Steven,
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-18 01:04:57 UTC
Permalink
Has anyone else answered this question? I haven't seen a response, but
perhaps I missed it.

More comments below.
Post by Ltc Hotspot
Hi Steven,
Question: What sorted function should I write to produce the desired
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
You do a good job of showing your desired output, but not the input. The
simplest possible code that will output the above string is:

print("""04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
""")


but naturally if the input is something else, you will need to do more
work. Without knowing what the input is, there is no way to know what
work you need to do to it.
Post by Ltc Hotspot
count = dict()
fname = raw_input("Enter file name: ")#
handle = open (fname, 'r')#
address = line.split()[5]
Are you sure that's giving you the address? Given the output you show
below, the "address" looks like this:

11:11:52

which looks to me like you are splitting on a date/time, not an email
adress.
Post by Ltc Hotspot
line = line.rstrip()
The above line is useless. You don't do anything with line after this
point, so why bother?
Post by Ltc Hotspot
count[address] = count.get(address, 0) + 1
lst = list()
lst.append( (val, key) )
lst.sort(reverse=True)
print key,val
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
[snip]

Please don't show hundreds of lines of output. Reduce it to a few lines,
maybe a dozen at most. And again, without seeing the input, how are we
supposed to tell whether the output is wrong or not?

And, no, please don't attach a mbox file to your email. Construct your
own SHORT input data:

data = [
'From ***@xyz\n',
'line of text\n',
'From ***@bar.com\n',
'more text\n',
'more text\n',
'From ***@xyz \n',
'more text \n',
'From ***@xyz\n',
]


Then operate on the fake data:

for line in data:
...


But I strongly expect that the problem is that your data doesn't look
like what you think it looks like. The format of the "From " line in
mbox looks like this:

"From address day month date time year"


so when you split it and take the 5th element, you get the time, not the
address.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-18 08:41:37 UTC
Permalink
Post by Steven D'Aprano
Has anyone else answered this question? I haven't seen a response, but
perhaps I missed it.
There have been several further mails - some off list.
The upshot is that he has submitted what he believes is
a working solution to his assignment.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...