0

This is the program I have to sort my list is

sorted_PC = sorted(PC, key=lambda x: x[1])
print(sorted_PC[0])

However, when I run it the final output is not the largest value like I am looking for, sorted by the second column of the array.

Edit: The final output should be the model name of the highest scoring PC, so I would like sorted() to sort the values in ascending order by the second column

So this [['lynx', 15.364583333333332], ['highest', 100], ['9', 82.03125], ['6', 67.1875], ['4', 53.125]] is what the list looks like pre-sort, but ['lynx', 15.364583333333332] is being output. i would excpect ['highest', 100] to output

M. Reade
  • 1
  • 2
  • 1
    What is input? What is output? What is expected output? – DecaK Jun 07 '18 at 19:58
  • 1
    The code you have has varying output based on the user's input. Could you post the final value of `PC` and what you expect `sorted()` to do to it so that we can determine what the problem is? – Hans Musgrave Jun 07 '18 at 19:59
  • 1
    How you build the list isn't really as important as the final result of building the list if sorting is the issue. Just show the list you want to sort, the output you get after sorting the list, and the output you *expect* from sorting the list. – chepner Jun 07 '18 at 20:03
  • `sorted` by default sorts in _ASCENDING_ order, if you want the largest score `print(sorted_PC[-1])` does the job. If you WANT to sort in descending order, negate the value returned by `lambda` as in `lambda rec: -rec[1]` ፨ I forgot to mention, please get rid of the superfluous `import` statement. – gboffi Jun 07 '18 at 20:08

0 Answers0