In Python (3.8), I have 15 lists of the same length.
Each list contains floats.
Here for the example, I will pretend I have 3 lists of length 5 to demonstrate my problem:
List1 = [29.561801, 29.564141, 29.566480, 29.293966, 29.291252]
List2 = [26.602566, 22.752335, 22.755249, 22.754278, 22.756220]
List3 = [23.966560, 23.960471, 23.954381, 29.568819, 29.571159]
For each index, I want to find which list has the maximum value. And store the result in a new List. With my 3 example list from above, my output would be:
Result=[1, 1, 1, 3, 3]
Right now I have the following code to do this. It works, but it feels very tedious (especially since I have in reality 15 lists and not 3!).
maxValue = 0
listWithMaxValue=0
Result=[]
for i in range(len(List1)):
maxValue=List1[i]
listWithMaxValue=1
if(List2[i] > maxValue):
maxValue=List2[i]
listWithMaxValue=2
if(List3[i] > maxValue):
maxValue=List3[i]
listWithMaxValue=3
Result=np.append(Result, listWithMaxValue)
It feels that there must be a better way to achieve the same result without repeating 15 times the same "if" loop?
Having variables
List1,List2,List3(presumably etc) is definitely a complication. You should have a single array and loop over its indices. Then the rest should be fairly trivial.This extracts the lowest-numbered index in the cases where there is a tie.
(For brevity, this only uses 7 lists of length 7; but it should be obvious that the logic extends to any dimensions.)
Because Python's variables are always references, you can actually build the outer array as a list of references to your existing variables if refactoring your current code seems like an excessive change.
Array indices in Python are zero-based, so index 0 in the result means
List1.