[英]how to make a function that receives a list of lists of numbers and returns the list has the highest number value in it?
def find_biggest(lst):
my_list = []
my_new_list = []
我该如何从这里开始,我从什么开始才能达到所有列表中的最大数字?
例如: find_biggest([[1, 2, 3], [10, -2], [1, 1, 1, 1]])
→ 是列表[10, -2]
因为10
是所有列表
您可以使用key
等于len
的max
函数来获取具有最大元素列表项的子列表
return max(lst, key = max)
您可以使用类似这样的方法来遍历列表列表中的所有列表并返回具有最大值的列表:
def find_biggest(list_of_lists):
biggest_num = float('-inf')
index_of_list_with_biggest = 0
for i in range(len(list_of_lists)):
current_list = list_of_lists[i]
for item in current_list:
if(item > biggest_num):
biggest_num = item
index_of_list_with_biggest = i
return list_of_lists[index_of_list_with_biggest]
if __name__ == "__main__":
print(find_biggest([[1, 2, 3], [10, -2], [1, 1, 1, 1]]))
输出:
[10, -2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.