Home > database >  How does the set function works in python with complex iterators like for and in
How does the set function works in python with complex iterators like for and in

Time:06-17

Please if anyone can explain me how this code works, it would be very helpful. Question :

Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order their names alphabetically and print each one on a new line. There are students in this class whose names and grades are assembled to build the following list:

python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

The lowest grade of belongs to Tina. The second lowest grade of belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.

Answer

    marksheet = []

    for _ in range(0,int(input())):
        marksheet.append([input(), float(input())])
    second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
    print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))

Result :

    Berry
    Harry

Can anyone please explain how the second highest is calculated. I understand that set function splits the data into distinct values and removes duplicates but what is like marks for name, marks in marksheet I am confused as marks and name are not even mentioned any where. How this second highest works. THANKS IN ADVANCE

CodePudding user response:

If marksheet contains python_students, then doing

for name, marks in marksheet unpacks each element of marksheet (in our case for example ['Harry', 37.21]) into the variables name and marks, such that name would get assigned the value "Harry" and marks would get the value 37.21

CodePudding user response:

You can break this down into individual steps as follows. First:

[marks for name, marks in marksheet]

This is a list comprehension. It produces a list of just the marks in marksheet. It does this by iterating over the elements of marksheet, assigning the two components of each element into name and marks, and then saving the marks value into a list.

Next, it creates a set from this:

set([marks for name, marks in marksheet])

This converts the list into a set, with duplicate values being removed.

It then converts the set back into a list, and then sorts the list:

sorted(list(set([marks for name, marks in marksheet])))

Since the list is sorted, with no duplicates, the second value in the list (at index 1) is the second-highest value. So it uses index 1 to extract this value, and assigns it to second_highest:

second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]

After that, it just iterates over marksheet (using another comprehension), saving the name from every pair with the second-highest mark. It joins the result with a newline as a separator, and prints the result, which will be those names, one per line.

CodePudding user response:

According to the problem statement you mentioned in the question, it should be named second_lowest and not second_highest.

set() is a data structure in python which keeps only unique values from the object which you pass to it.

for name, marks in marksheet is a short way of unpacking the elements of marksheet. marksheet is a list whose elements are lists themselves and those elements have two entries, name and marks. You can also write a verbose version like:

for entry in marksheet:
    name = entry[0]
    marks = entry[1]
    # add more operations that you want to perform
    ...

The set of marks that you've extracted from your list marksheet is then converted into a list using list() function in order to be able to sort your list containing unique marks from marksheet using sorted() function.

.join() joins the elements of your list of names (created using another list comprehension- a for a,b in sorted(marksheet) if b == second_highest) having second lowest marks in the class and adds a newline(\n) between those elements.

My two cents is that if you're not so comfortable with list comprehensions and unpacking as of now. You should approach things the simpler way or take some time out to learn more about these and get comfortable with them before using it.

I'm linking two good resources for learning list comprehensions and unpacking, in case you're interested-

  • Related