AttributeError

Denis McMahon denismfmcmahon at gmail.com
Wed Aug 12 13:36:20 CEST 2015


On Tue, 11 Aug 2015 17:01:24 -0700, Ltc Hotspot wrote:

> 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

At this point, count seems to be a dictionary of address: count of lines

> for key,val in count.items():
>     ncount = (key,val) ncount.sort(reverse=True) print key,val

ncount is a single key-value pair. Why are you trying to sort ncount?

Do you want results ordered by count?

First, change your dictionary into a list of tuples:

ncount = [(a,c) for a,c in count.items()]

Then sort ncount on the second field of the tuple:

ncount.sort(key = lambda x: x[1], reverse=True)

print ncount

-- 
Denis McMahon, denismfmcmahon at gmail.com


More information about the Python-list mailing list