您好,登錄后才能下訂單哦!
本文實例講述了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程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。