Define a function sort_grades(tup1). The argument tup1 is a tuple each composed of (student_name, score). The function returns a tuple with same elements of tup1 but sorted by their scores in descending order.Exampletup1 = (('Bob', 47), ('Claire', 61), ('Dominic', 79))Expected output:(('Dominic', 79), ('Claire', 61), ('Bob', 47))Hintfirst convert tup1 to a list. Since each element in this list is a tuple with two items (student_name, score), to sort the list according to the second item score, you can use Python built-in sorted function:sorted(iterable, key=None, reverse=False) function returns a sorted list from the iterable object(refer to similar examples in Chapter 4.1).convert the above list to a tuple again, and return the tuple
Question
Define a function sort_grades(tup1). The argument tup1 is a tuple each composed of (student_name, score). The function returns a tuple with same elements of tup1 but sorted by their scores in descending order.Exampletup1 = (('Bob', 47), ('Claire', 61), ('Dominic', 79))Expected output:(('Dominic', 79), ('Claire', 61), ('Bob', 47))Hintfirst convert tup1 to a list. Since each element in this list is a tuple with two items (student_name, score), to sort the list according to the second item score, you can use Python built-in sorted function:sorted(iterable, key=None, reverse=False) function returns a sorted list from the iterable object(refer to similar examples in Chapter 4.1).convert the above list to a tuple again, and return the tuple
Solution
Sure, here is a step-by-step guide on how to define the function sort_grades(tup1):
- First, you need to convert the tuple
tup1to a list. This is because tuples in Python are immutable, meaning they cannot be changed once created. On the other hand, lists are mutable and can be sorted. You can convert the tuple to a list by using thelist()function. Let's call this listlist1. Here is how you can do it:
list1 = list(tup1)
-
Next, you need to sort
list1according to the scores. You can use the built-insorted()function in Python. Thesorted()function takes three parameters:iterable,keyandreverse.iterableis the sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be sorted.keyis a function that serves as a key or a basis of sort comparison.reverseis a boolean value. If set to True, then the iterable would be sorted in reverse (descending) order.
In your case,
iterableislist1,keyshould be a function that returns the second element of the tuple (i.e., the score), andreverseshould beTruebecause you want to sort the scores in descending order. Here is how you can do it:
list1 = sorted(list1, key=lambda x: x[1], reverse=True)
- Finally, you need to convert
list1back to a tuple and return it. You can convert the list to a tuple by using thetuple()function. Here is how you can do it:
return tuple(list1)
So, the complete function sort_grades(tup1) would look like this:
def sort_grades(tup1):
list1 = list(tup1)
list1 = sorted(list1, key=lambda x: x[1], reverse=True)
return tuple(list1)
You can test this function with your example:
tup1 = (('Bob', 47), ('Claire', 61), ('Dominic', 79))
print(sort_grades(tup1)) # prints (('Dominic', 79), ('Claire', 61), ('Bob', 47))
Similar Questions
Define a function sort_list(list1). The function returns three lists: the original list, the list in ascending order, and the list in descending order. Remember: your function should return three lists.Examplelist1 = [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]Expected output:[10, 1, 9, 2, 8, 3, 7, 4, 6, 5][1, 2, 3, 4, 5, 6, 7, 8, 9, 10][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Suppose a courses score of a class is saved in a dictionarydata = {'Tom': [84, 96, 90], 'Bob':[ 67, 82, 76], 'Jack': [62, 90, 67], 'Linda': [78, 66,90]}Write a function to calculate the average score for each student, round the float value to integer, and save the average score in a dictionary, and then sort the dictionary based on the average score in reverse order and save in a list. return the dictionary and list.output:({'Tom': 90, 'Bob': 75, 'Jack': 73, 'Linda': 78},[('Tom', 90), ('Linda', 78), ('Bob', 75), ('Jack', 73)])
You work in the examination department of a school, and you've been given a task to sort the test scores of students in ascending order. The scores are stored in a list where each element represents the score of a student.Input FormatThe first line contains the integer N, the size of the array. The next line contains N space-separated integers.Constraints• 1<=N<=1000 • -1000<=a[i]<=1000Output FormatPrint the array as a row of space-separated integers in each iteration.Sample Input 01010 9 8 7 6 5 4 3 2 1Sample Output 05 9 8 7 6 10 4 3 2 15 4 8 7 6 10 9 3 2 15 4 3 7 6 10 9 8 2 15 4 3 2 6 10 9 8 7 15 4 3 2 1 10 9 8 7 63 4 5 2 1 10 9 8 7 63 2 5 4 1 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 8 9 10 7 61 2 3 4 5 8 7 10 9 61 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
What is the output of the Python code below?my_tup = (3, 2, 1, 2)print(tuple(sorted(my_tup)))Question 10Select one:a.(1, 2, 3)b.{1, 2, 2, 3}c.(1, 2, 2, 3)d.syntax errore.[1, 2, 2, 3]
Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), find the top 100 students based on their average grades sorted descendingly by the average grade and in case multiple students have the same average grade, sort them lexicographically in ascending order by their names. Your query should output a table with the following columns (name, average_grade).
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.