Home > Back-end >  Matching 2 different lists with highest values in Python
Matching 2 different lists with highest values in Python

Time:09-19

Array_student = [Joe, Peter, Andy, Tom, Pat]

Array_marks = [14, 9, 6, 8, 12]

Find the highest marks in the array and print the name of the student with the highest marks.

Without using any function such as max().

CodePudding user response:

You should use {} dict where the student name will be the key and the marks will be the value and then use sorting and take the first item of the new list

data = {'joe':14, 'Peter': 9, 'Andy': 6, 'Tom' : 8, 'Pat' : 12}
sort_data = sorted(data.items(), key=lambda x: x[1], reverse=True)
next(iter(sort_data)) # outputs 'Peter'

CodePudding user response:

Array_student = ["Joe", "Peter", "Andy", "Tom", "Pat"]
Array_marks = [14, 9, 6, 8, 12]

greatest_point = 0
greatest_student = ""
for idx,mark in enumerate(Array_marks):
    if mark > greatest_point:
        greatest_point = mark
        greatest_student = Array_student[idx]
print(greatest_student) #Joe
  • Related