[Tutor] AttributeError,

Alan Gauld alan.gauld at btinternet.com
Wed Aug 12 02:39:37 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'?

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.

> 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)

use

ncount = sorted(ncount, reverse=True)

>      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




More information about the Tutor mailing list