0

Nested list

Trying to solve a challenge where I make a nested list giving the second highest score. Input should be a name and a float. Keeps giving me an error message, but I cannot understand what's wrong. Any help appreciated:). marksheet = [] for _ in range(0,int(input())): marksheet.append([input(), float(input())]) second_highest = sorted(list(set([marks for name, marks in marksheet]))) print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))

23rd May 2019, 3:50 AM
tristach605
tristach605 - avatar
7 odpowiedzi
+ 1
This fixes your code, just keep in mind that you need at least two marks so it doesn't throw an index error. second_highest = sorted(list(set([marks for name, marks in marksheet])))[-2]
23rd May 2019, 4:02 AM
Diego
Diego - avatar
+ 1
1. Inputs should be written in separate lines. alice 9.42 bob 9.14 2. The sorted() functions returns a new sorted list of the items of the argument. https://docs.python.org/3/library/functions.html#sorted 3. In your code, you assign an ascending sorted list to "second_highest". For example: print(second_highest) # [7.00, 8.00, 9.00, 10.00] The element in the (-1) position, the last element, is the greatest number. The element in the (-2) position, the second to last element, is the second greatest number. Doing "sorted(list(...))[-2]" assigns the element in the (-2) position, not the whole list, to "second highest".
23rd May 2019, 11:36 PM
Diego
Diego - avatar
+ 1
Thanks, Diego! I'll re-work that section now👍🏻
24th May 2019, 2:12 AM
tristach605
tristach605 - avatar
+ 1
Try this as input (in separate lines). 3 alice 9.42 bob 9.12 charlie 9.56
24th May 2019, 2:38 AM
Diego
Diego - avatar
0
Thanks Diego. So youre defining "second highest" and saying it's equal to a sorted list, with two cariables (names and marks)? Is that correct? If so, what does the -2 do? Also, could you explain how the "sorted" command works (looked in the Python lessons but couldnt find it)? Thanks again Diego and appreciate your help (as always:)).
23rd May 2019, 10:40 PM
tristach605
tristach605 - avatar
0
Tried the code again (with fix): got the following error message. Any help appreciated (code abd error message below): marksheet = [] for _ in range(0,int(input())): marksheet.append([input(), float(input())]) second_highest = sorted(list(set([marks for name, marks in marksheet])))[-2] Traceback (most recent call last): File "..\Playground\", line 2, in <module> for _ in range(0,int(input())): ValueError: invalid literal for int() with base 10: 'dad,2.00'
23rd May 2019, 10:47 PM
tristach605
tristach605 - avatar
0
Still getting the same error message:(. I entered on two separare lines and tried reversing the input (entering the numbers first)? Any other suggestions? Traceback (most recent call last): File "..\Playground\", line 2, in <module> for _ in range(0,int(input())): ValueError: invalid literal for int() with base 10: 'bob'
24th May 2019, 2:35 AM
tristach605
tristach605 - avatar