Python使用min、max函数查找二维数据矩阵中最小、最大值的方法
作者:Together_CZ
本文实例讲述了Python使用min、max函数查找二维数据矩阵中最小、最大值的方法。分享给大家供大家参考,具体如下:
简单使用min
、max
函数来得到二维数据矩阵中的最大最小值,很简单,这是因为工作需要用到一个东西所以先简单来写了一下:
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出来随机生成矩阵中的最大、最小值 ''' import time import random def random_matrix_genetor(n=10): ''''' 功能:生成随机矩阵 输入:矩阵维数 输出:矩阵 ''' data_matrix=[] for i in range(n): one_list=[] for j in range(n): one_list.append(random.randint(1, 100)) data_matrix.append(one_list) return data_matrix def find_martrix_min_value(data_matrix): ''''' 功能:找到矩阵最小值 ''' new_data=[] for i in range(len(data_matrix)): new_data.append(min(data_matrix[i])) print 'data_matrix 最小值为:', min(new_data) def find_martrix_max_value(data_matrix): ''''' 功能:找到矩阵最大值 ''' new_data=[] for i in range(len(data_matrix)): new_data.append(max(data_matrix[i])) print 'data_matrix 最小值为:', max(new_data) if __name__ == '__main__': data_matrix=random_matrix_genetor(20) print data_matrix find_martrix_min_value(data_matrix) find_martrix_max_value(data_matrix)
结果如下:
[[44, 36, 51, 27, 29, 92, 27, 33, 79, 10, 40, 35, 56, 61, 69, 87, 94, 48, 41, 28], [79, 85, 33, 46, 87, 39, 84, 92, 8, 82, 41, 12, 91, 52, 66, 15, 13, 59, 54, 62], [60, 71, 57, 31, 65, 60, 78, 43, 97, 35, 27, 34, 24, 77, 25, 65, 25, 3, 23, 65], [68, 87, 38, 22, 70, 48, 61, 36, 22, 70, 34, 23, 96, 14, 72, 82, 2, 71, 32, 26], [20, 60, 77, 74, 86, 21, 48, 89, 74, 14, 82, 88, 91, 14, 17, 11, 86, 49, 68, 70], [8, 69, 90, 52, 79, 13, 60, 56, 55, 29, 8, 13, 24, 68, 71, 10, 99, 56, 65, 96], [67, 86, 79, 52, 96, 56, 77, 49, 18, 18, 1, 84, 97, 28, 64, 37, 89, 57, 66, 54], [64, 44, 62, 65, 67, 89, 84, 28, 13, 65, 27, 19, 10, 96, 15, 76, 44, 15, 32, 67], [2, 30, 79, 47, 11, 61, 96, 95, 95, 1, 64, 98, 75, 22, 90, 53, 14, 77, 29, 65], [43, 100, 65, 76, 10, 42, 70, 82, 51, 72, 76, 67, 94, 11, 73, 55, 52, 3, 50, 6], [26, 95, 22, 93, 96, 74, 56, 41, 45, 67, 70, 3, 12, 77, 11, 39, 79, 76, 95, 32], [21, 59, 55, 93, 18, 38, 5, 66, 39, 76, 90, 91, 21, 3, 48, 34, 58, 60, 31, 16], [58, 21, 56, 24, 31, 20, 38, 98, 53, 27, 24, 85, 73, 2, 39, 64, 40, 33, 22, 98], [63, 97, 80, 26, 50, 84, 77, 39, 90, 95, 46, 93, 32, 100, 74, 26, 47, 53, 13, 61], [30, 48, 27, 9, 73, 35, 58, 81, 23, 59, 71, 24, 89, 5, 81, 55, 15, 66, 65, 93], [29, 55, 53, 27, 79, 32, 74, 23, 73, 15, 52, 10, 91, 37, 12, 93, 69, 74, 96, 64], [13, 68, 53, 64, 99, 15, 82, 3, 3, 46, 20, 88, 23, 78, 46, 78, 61, 73, 48, 98], [14, 25, 91, 78, 17, 42, 23, 87, 47, 98, 65, 62, 62, 37, 63, 54, 75, 59, 23, 89], [96, 47, 90, 71, 15, 95, 45, 58, 24, 12, 55, 72, 25, 66, 79, 44, 57, 15, 89, 43], [57, 100, 10, 44, 3, 67, 21, 60, 99, 78, 50, 74, 23, 49, 74, 87, 66, 53, 60, 67]]
data_matrix 最小值为: 1
data_matrix 最小值为: 100
[Finished in 0.3s]
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。