在二维数组中查找最大值 [英] Finding the Max value in a two dimensional Array

查看:125
本文介绍了在二维数组中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种优雅的方法来在二维数组中找到最大值. 例如此数组:

I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array:

[0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0]

我想提取值"4". 我想在max范围内做一个max,但是我在执行它时很挣扎.

I would like to extract the value '4'. I thought of doing a max within max but I'm struggling in executing it.

推荐答案

最大数量的最大值(map(max, numbers)产生1,2,2,3,4):

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers))  # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers))  # max of those max-numbers
4

这篇关于在二维数组中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆