0

I have a list, and it is currently sorted by key(A,B,C). The value is a number count of how many times the key appears in a file. The code gives results like this:

14 A
157 B
17 C
...

I need to reverse this order around. I want to sort by the value instead of key so it reads like this:

14 A
17 C
157 B

I have read and tried all the documentation I could find on here and Google. I don't think it should be too complex, but I am overthinking something.

Is there a simple solution to read the order by number value? Also every time I try to sort by value, it says 'int or str is not callable'. I don't understand what this means.

Some of my code is below:

lst = list()
for key, val in counts.items():
    lst.append((key, val))
lst.sort()    

for key, val in lst:
   print val, key
Maria Gosur
  • 91
  • 2
  • 12

3 Answers3

1

The key argument to sort() allows you to specify the sorting key.

You could use:

lst.sort(key=lambda (k,v):v)

or, equivalently,

lst.sort(key=operator.itemgetter(1))
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thank you so much for your help NPE. I really appreciate it. The first line suggestion worked well. However, I don't know that I fully grasp what that line is reading. Can you explain the syntax of the lambda and :v being used? – Maria Gosur Oct 12 '13 at 16:04
0

list.sort accept optional key parameter.

>>> counts = {
...     'A': 14,
...     'B': 157,
...     'C': 17,
... }
>>>
>>> lst = counts.items()
>>> lst.sort(key=lambda x: x[1]) # x[1] => count
>>> for key, val in lst:
...     print val, key
...
14 A
17 C
157 B

According to Mutable Sequence Types:

The sort() method takes optional arguments for controlling the comparisons.

...

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thank you falsetru, this solution also works. Can you explain the line and syntax for lst.sort(key=lambda x: x[1])? How does this read? Is the [1] representing the value's position in the list? – Maria Gosur Oct 12 '13 at 16:06
  • @MariaGosur, `lambda` is a kind of anonymous function. `lambda x: x[1]` is a function that return the 2nd item of the passed parameter. `x` here is each item of the list. – falsetru Oct 12 '13 at 16:08
  • ah okay. I get it in brief overview. I have to spend more time studying the lambda in practice, though I've vaguely used it. Makes sense though in essence. Thank you for explaining it. This helps a lot. – Maria Gosur Oct 12 '13 at 16:11
  • I'm still getting used to Stackoverflow. I had to start a new account and lost all my rep points. It's giving me a hard time accepting the answers, but I like your answer! :) – Maria Gosur Oct 12 '13 at 16:13
0

This works, assuming

import operator
for key, val in counts.items():
  lst.append((key, val))
# lst should be like this :
# lst = [('A',14), ('B',157), ('C',17), ('D',12), ('E',189)]
sorted_lst = sorted(set(lst), key=operator.itemgetter(1))
print sorted_lst
Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39
  • Thank you fjammes. I have not dove too deep into the python imports, but I will look further into this. I appreciate and value everyone's help! – Maria Gosur Oct 12 '13 at 16:07