AttributeError

MRAB python at mrabarnett.plus.com
Wed Aug 12 11:12:45 EDT 2015


On 2015-08-12 06:03, Ltc Hotspot wrote:
> Message heard loud and clear:
>
> There are no error messages, the output is the issue.
>
> Question: What sorted function should I write to produce the desired
> output, below:
>
Instead of iterating over "count.items()", iterate over 
"sorted(count.items())".

> 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
>
[snip]
I don't know why you have a nested 'for' loop; just iterate over the
sorted items and print them. Simple.




More information about the Python-list mailing list