[Tutor] AttributeError,
Alan Gauld
alan.gauld at btinternet.com
Wed Aug 12 02:45:08 CEST 2015
On 12/08/15 00:24, Ltc Hotspot wrote:
> 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...
> 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)
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.
> 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
More information about the Tutor
mailing list