How to extract index position of minimum list value?

Viewed 78

So, I have this code. How to get the index of the minimum list value

import numpy as np
dict1={"num1":(9,6,6,9),"num2":(9,9,6,6)}
testnum=(10,5,5,10)
listdict= list(map(lambda x:x, dict1.values()))
result = np.array(listdict)-np.array(testnum)
print("result:",result)
print("len :",len(result))

print:

result: [[-1  1  1 -1]
       [-1  4  1 -4]]
len: 2

By just viewing it, I can point that index 0 of result has the minimum value. How to get the index position? Any idea on this?

target:

min [[-1  1  1 -1]
index 0
2 Answers

You can print the position of the min of a list without much extra manipulation:

testnum = (1,2,10,20,10,5)
print(testnum.index(min(testnum)))

Try using the min function:

import numpy as np
dict1={"num1":(9,6,6,9),"num2":(9,9,6,6)}
testnum=(10,5,5,10)
listdict= list(map(lambda x:x, dict1.values()))
result = min(np.array(listdict)-np.array(testnum), key=sum)
index = (np.array(listdict)-np.array(testnum)).tolist().index(result.tolist())
print("result:",result)
print("index:",index)

Output:

result: [-1  1  1 -1]
index: 0
Related