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.
此代码应该迭代列表列表并返回包含最小值的整个列表。我已经确定它一直在索引[0]返回列表,但我无法弄清楚原因。任何帮助,甚至提示,将不胜感激。
def list_with_min(list_of_lists):
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]]))
# [9, 10, 15]--------> should be [-3, 7, 8]
print(list_with_min([[5], [9], [6], [2], [7], [10], [72]]))
# [5]----------------> should be [2]
print(list_with_min([[-2, 6, 9], [-9, 6, 9], [4, 8, 2], [5, -2]]))
# [-2, 6, 9]---------> should be [[-2, 6, 9], [5, -2]] (I assume two lists with the same minimum value should both be returned?)
5 个解决方案
#1
1
You can provide a key to the function min
, that is a function used for comparison. It turns out that here you want key to be the function min
itself.
您可以为函数min提供一个键,这是一个用于比较的函数。事实证明,在这里你想要键是min本身的函数。
list_of_lists = [[9, 10, 15], [1, 8, 4], [-3, 7, 8]]
min(list_of_lists, key=min) # [-3, 7, 8]
This does not return multiple minima, but can be improved to do so.
这不会返回多个最小值,但可以进行改进。
list_of_lists = [[9, 10, 15], [1, -3, 4], [-3, 7, 8]]
min_value = min(map(min, list_of_lists))
[lst for lst in list_of_lists if min(lst) == min_value] # [[1, -3, 4], [-3, 7, 8]]
#2
0
you can get this in one line with a list comprehension (ive added three though to help you work through the logic), it deals with duplicates differently however:
你可以通过列表理解在一行中得到这个(为了帮助你完成逻辑,我添加了三个),它以不同的方式处理重复:
#for each list return a list with the minimum and the list
mins_and_lists = [[min(_list), _list] for _list in lists]
#find the minimum one
min_and_list = min([[min(_list), _list] for _list in lists])
#parse the result of the minimum one list
minimum, min_list = min([[min(_list), _list] for _list in lists])
if you want to handle duplicate minimums by returning both then:
如果你想通过返回两者来处理重复的最小值,那么:
dup_mins = [_list for _min, _list in mins_and_lists if _min == minimum]
#3
0
EDIT: A more python way could be this:
编辑:一个更python的方式可能是这样的:
def list_with_min(l): min_vals = [min(x) for x in l] return l[min_vals.index(min(min_vals))]
A bit bulky but it works...
有点笨重,但它的工作原理......
l=[[9, 10, 15], [1, 8, 4], [-3, 7, 8]]
def list_with_min(l):
m = min(l[0])
for i in l[1:]:
m = min(i) if min(i) < m else m
for i in l:
if m in i:
return i
print(list_with_min(l))
Output:
[-3, 7, 8]
#4
0
You could also use this:
你也可以用这个:
min([ (min(a),a) for a in list_of_lists ])[1]
#5
-1
Your condition just makes no sense. You're checking
你的病情没有任何意义。你正在检查
if min_list < list_i[0]:
which means, if the smallest value of list_i is less than the first value of list_i.
这意味着,如果list_i的最小值小于list_i的第一个值。
I don't think you'd ever want to compare to just list_i[0]. You need to store min_list across loops, and compare to that.
我认为你不想只与list_i [0]进行比较。您需要跨循环存储min_list,并与之进行比较。
#1
1
You can provide a key to the function min
, that is a function used for comparison. It turns out that here you want key to be the function min
itself.
您可以为函数min提供一个键,这是一个用于比较的函数。事实证明,在这里你想要键是min本身的函数。
list_of_lists = [[9, 10, 15], [1, 8, 4], [-3, 7, 8]]
min(list_of_lists, key=min) # [-3, 7, 8]
This does not return multiple minima, but can be improved to do so.
这不会返回多个最小值,但可以进行改进。
list_of_lists = [[9, 10, 15], [1, -3, 4], [-3, 7, 8]]
min_value = min(map(min, list_of_lists))
[lst for lst in list_of_lists if min(lst) == min_value] # [[1, -3, 4], [-3, 7, 8]]
#2
0
you can get this in one line with a list comprehension (ive added three though to help you work through the logic), it deals with duplicates differently however:
你可以通过列表理解在一行中得到这个(为了帮助你完成逻辑,我添加了三个),它以不同的方式处理重复:
#for each list return a list with the minimum and the list
mins_and_lists = [[min(_list), _list] for _list in lists]
#find the minimum one
min_and_list = min([[min(_list), _list] for _list in lists])
#parse the result of the minimum one list
minimum, min_list = min([[min(_list), _list] for _list in lists])
if you want to handle duplicate minimums by returning both then:
如果你想通过返回两者来处理重复的最小值,那么:
dup_mins = [_list for _min, _list in mins_and_lists if _min == minimum]
#3
0
EDIT: A more python way could be this:
编辑:一个更python的方式可能是这样的:
def list_with_min(l): min_vals = [min(x) for x in l] return l[min_vals.index(min(min_vals))]
A bit bulky but it works...
有点笨重,但它的工作原理......
l=[[9, 10, 15], [1, 8, 4], [-3, 7, 8]]
def list_with_min(l):
m = min(l[0])
for i in l[1:]:
m = min(i) if min(i) < m else m
for i in l:
if m in i:
return i
print(list_with_min(l))
Output:
[-3, 7, 8]
#4
0
You could also use this:
你也可以用这个:
min([ (min(a),a) for a in list_of_lists ])[1]
#5
-1
Your condition just makes no sense. You're checking
你的病情没有任何意义。你正在检查
if min_list < list_i[0]:
which means, if the smallest value of list_i is less than the first value of list_i.
这意味着,如果list_i的最小值小于list_i的第一个值。
I don't think you'd ever want to compare to just list_i[0]. You need to store min_list across loops, and compare to that.
我认为你不想只与list_i [0]进行比较。您需要跨循环存储min_list,并与之进行比较。