查找矩阵的最大值和最小值

Jon*_*ter 8 max matrix return-value min python-3.x

我在python 3中编写了这段代码:

matrix = []
    loop = True
    while loop:
        line = input()
        if not line: 
            loop = False
        values = line.split()
        row = [int(value) for value in values]
        matrix.append(row)

    print('\n'.join([' '.join(map(str, row)) for row in matrix]))
    print('matrix saved')
Run Code Online (Sandbox Code Playgroud)

返回矩阵的一个例子是 [[1,2,4],[8,9,0]]。我想知道如何找到矩阵的最大值和最小值?我尝试了 python 的 max(matrix) 和 min(matrix) 内置函数,但它不起作用。

谢谢你的帮助!

eug*_*gen 10

单线:

最大:

matrix = [[1, 2, 4], [8, 9, 0]]
print (max(map(max, matrix))
9
Run Code Online (Sandbox Code Playgroud)

分钟:

print (min(map(min, matrix))
0
Run Code Online (Sandbox Code Playgroud)


A.J*_*pal 2

使用内置函数max()min()在剥离列表列表后:

matrix = [[1, 2, 4], [8, 9, 0]]
dup = []
for k in matrix:
    for i in k:
        dup.append(i)

print (max(dup), min(dup))
Run Code Online (Sandbox Code Playgroud)

运行如下:

>>> matrix = [[1, 2, 4], [8, 9, 0]]
>>> dup = []
>>> for k in matrix:
...     for i in k:
...         dup.append(i)
... 
>>> print (max(dup), min(dup))
(9, 0)
>>> 
Run Code Online (Sandbox Code Playgroud)