使用Python查詢矩陣中每一行的最大元素


在本文中,我們將學習一個Python程式,用於查詢矩陣中每一行的最大元素。

假設我們已經得到一個NxN的輸入矩陣。我們將使用以下方法查詢輸入矩陣中每一行的最大元素。

使用的方法

以下是完成此任務的各種方法:

  • 使用巢狀for迴圈

  • 使用max()函式

  • 使用map()和lambda函式

方法1:使用巢狀for迴圈

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個函式maximumRowElement(),透過接受輸入矩陣作為引數來列印輸入矩陣每一行的最大元素。

  • 使用len()函式計算矩陣的長度,以獲取矩陣的行數。

  • 透過計算矩陣中任意一行的長度來獲取矩陣的列數。

  • 使用for迴圈遍歷矩陣的行。

  • 使用一個變數儲存最大行元素,並將其初始化為0。

  • 使用另一個巢狀的for迴圈遍歷矩陣的所有列。

  • 檢查當前元素值是否大於上述max變數。

  • 如果上述條件為真,則將此元素設定為最大值。

  • 列印最大值變數。

  • 建立一個變數來儲存輸入矩陣,並使用巢狀for迴圈列印給定的矩陣。

  • 透過將輸入矩陣作為引數傳遞給上述定義的maximumRowElement()函式,列印矩陣中每一行的最大元素。

示例

以下程式使用巢狀for迴圈返回輸入矩陣每一行的最大元素:

# creatind a function to get the maximum element in a
# row of a matrix by accepting the input matrix as an argument
def maximumRowElement(inputMatrix):
   # getting the number of rows of the given matrix
   rows = len(inputMatrix)
   # getting the number of columns of the given matrix
   cols = len(inputMatrix[0])
   # traversing through the rows of the matrix
   for p in range(rows):
      # initializing maximum with 0 for finding a maximum element of each row
      maximum = 0
      # traversing through all the columns of a matrix
      for q in range(cols):
         # checking whether the current element value is greater than the max value
            if inputMatrix[p][q] > maximum:
               # assigning that element to the maximum now it becomes the maximum element
               maximum = inputMatrix[p][q]
      # printing the maximum element of each row of a matrix
      print(maximum)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]

print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
   # traversing through all the columns of the current row
   for n in range(len(inputMatrix[0])):
      # printing the corresponding element
      print(inputMatrix[m][n], end=' ')
   print()
print("maximum element in each row of an input matrix is:")
# calling the maximumRowElement() function by passing
# input matrix to it
maximumRowElement(inputMatrix)

輸出

執行上述程式後,將生成以下輸出:

The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 
maximum element in each row of an input matrix is:
5
11
7
4

方法2:使用max()函式

max()方法(返回迭代器中值最高的專案/最大數字)

示例

以下程式使用max()函式返回輸入矩陣每一行的最大元素:

# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
   for n in range(len(inputMatrix[0])):
      # printing the corresponding element
      print(inputMatrix[m][n], end=' ')
   print()

print("maximum element in each row of an input matrix is:")
# traversing through each row of elements of the  matrix
for k in inputMatrix:
   # printing the maximum element in a row using the max() function
      print(max(k))

輸出

The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 
maximum element in each row of an input matrix is:
5
11
7
4

方法3:使用map()和lambda函式

使用內建的map()函式是另一種可以用於獲取矩陣每一行中最大元素的方法。map()方法以確定的方式(例如列表或矩陣)對迭代器的每個元素應用指定的函式。在這種情況下,map()方法可以用來對矩陣的每一行應用max()函式。

示例

以下程式使用map()和lambda函式返回輸入矩陣每一行的最大元素:

# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
   # traversing through all the columns of the current row
   for n in range(len(inputMatrix[0])):
      # printing the corresponding element
      print(inputMatrix[m][n], end=' ')
   print()

print("maximum element in each row of an input matrix is:")
# Applying max() function to each list element(row) to get maximum value
result = list(map(lambda row: max(row), inputMatrix))
# Traversing in the result list
for i in result:
   # Printing the maximum row element
   print(i)

輸出

The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 
maximum element in each row of an input matrix is:
5
11
7
4

結論

在本文中,我們學習了三種不同的方法來列印給定矩陣中每一行的最大值。我們還學習瞭如何使用map()函式對每個迭代器應用特定條件。

更新於:2023年1月23日

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告