查找矩阵的最大值和最小值 [英] Find maximum and minimum value of a matrix

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

问题描述

我将这段代码写在python 3中:

I have this code wrote in 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')

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

an example of returned matrix would be [[1,2,4],[8,9,0]].Im wondering of how I could find the maximum and minimum value of a matrix? I tried the max(matrix) and min(matrix) built-in function of python but it doesnt work.

感谢您的帮助!

推荐答案

在删除列表列表后,使用内置函数max()min():

Use the built-in functions max() and min() after stripping the list of lists:

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

print (max(dup), min(dup))

运行方式为:

>>> 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)
>>> 

这篇关于查找矩阵的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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