Return the lowest value - Python

0
from collections import OrderedDict #Pra poder acessar os elementos igual listas e tuplas.
                              v                 v                 v
lista_aberta = {(1,3): [1,2,3,4], (1,4): [2,3,4,9], (1,5): [3,4,1,7]}

I need to check all the elements and return which has the smallest [3] value, which in this case is (1,3) where its [3] value is equal to 4.

I use a repetition structure to compare values, or do you have a more direct method to do this?

    
asked by anonymous 11.04.2018 / 18:29

1 answer

1

I do not know of any "direct method" that is ready for this, unless you obviously develop it yourself.

In any case, you would use something similar to this, creating a method for reuse and thus using a "direct method". Using sys.maxsize (for python2 use sys.maxint ) to ensure that you always get the lowest value since you are using the largest integer value to compare initially and accessing with repeat loop your choice the for to be clearer what is happening).

import sys

lista_aberta = {(1,3): [1,2,3,4], (1,4): [2,3,4,9], (1,5): [3,4,1,7]}

menor: int = sys.maxsize
objMenor = {}
for key in lista_aberta:
    if lista_aberta[key][3] < menor:
        menor = lista_aberta[key][3]
        objMenor = {key : lista_aberta[key]}

print('Menor valor da lista é: ', menor, 'Obj ->', objMenor)
    
11.04.2018 / 19:46