How to print out the item of a list when its corresponding value in another list is the biggest?

Question:

If I have 2 lists:

values = ['a', 'b', 'c', 'd']
accuracies = [1, 2, 3, 4]

I want to return the value in values when its corresponding accuracy in accuracies is the biggest.
In this case, the result of value should be 'd', because its corresponding accuracy is 4.
Is there a way to do it in python?
Thank you in advance.

Asked By: Bella

||

Answers:

Use this:

In [5]: values[accuracies.index(max(accuracies))]
Out[5]: 'd'
Answered By: Mehrdad Pedramfar

Iterate through the accuracies list and find the largest value. Every time you find a larger value make sure to keep track of the "index" in which it occurs. If 4 is the largest value your program should know that index 3 is where the largest value is in accuracies. Once you have the index of the largest element, simply access values in the same spot.

values = ['a', 'b', 'c', 'd'] 
accuracies = [1, 2, 3, 4]

max_val = float('-inf')
max_val_index = 0
for index, acc in enumerate(accuracies):
    if acc > max_val:
        max_val = acc
        max_val_index = index

print(values[i])
Answered By: JRose

You can solve this by simply getting the max value the index of said value, then finding the equivalent in the values list

values[accuracies.index(max(accuracies))]

this returns in your case d

Answered By: Mateo Covacho
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.