jeudi 22 mars 2018

Finding a minimum value in a list of lists and returing that list [python]

This code is supposed to iterate over the list of lists and return the entire list that contains the smallest value. I have already identified that it keeps returning the list at index[0], but I cannot figure out why. Any help, or even hints, would be greatly appreciated.

def list_with_min(list_of_lists): ''' Returns the list containing the minimum value :param alist: a list of lists of numbers; must not be empty! :return: the list in the list of lists with the smallest value '''

m = 0

for i in range(len(list_of_lists)-1):
    list_i = list_of_lists[m]
    min_list = min(list_of_lists[m])

    if min_list < list_i[0]:
        m = i

answer = list_of_lists[m]
return answer

print(list_with_min([[9, 10, 15], [1, 8, 4], [-3, 7, 8]]))

print(list_with_min([[5], [9], [6], [2], [7], [10], [72]]))

print(list_with_min([[-2, 6, 9], [-9, 6, 9], [4, 8, 2], [5, -2]]))

returns [9, 10, 15]--------> should be [-3, 7, 8]

[5]----------------> should be [2]

[-2, 6, 9]---------> should be [[-2, 6, 9], [5, -2]] (I assume two lists with the same minimum value should both be returned?)

Aucun commentaire:

Enregistrer un commentaire