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.
CodePudding user response:
Use this:
In [5]: values[accuracies.index(max(accuracies))]
Out[5]: 'd'
CodePudding user response:
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])
CodePudding user response:
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