使用Python交換矩陣首尾兩行的元素


在這篇文章中,我們將學習一個Python程式,用於交換矩陣中首尾兩行的元素。

使用的方法

  • 使用臨時變數(使用巢狀迴圈)

  • 使用交換(,)運算子(無需迴圈)

  • 使用pop()、insert()和append()函式

方法1:使用臨時變數(使用巢狀迴圈)

演算法(步驟)

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

  • 建立一個函式swapFirstandLastRow(),透過接受輸入矩陣、行數和列數作為引數來交換輸入矩陣的首尾兩行。

  • 遍歷矩陣的行和列,使用臨時變數交換首尾兩行的元素。

  • 建立一個變數來儲存輸入矩陣。建立一個變數來儲存矩陣的輸入行數

  • 建立一個變數來儲存矩陣的輸入列數

  • 透過將輸入矩陣、行數和列數傳遞給它來呼叫上面定義的swapFirstandLastRow()函式,以交換首尾兩行。

  • 使用巢狀for迴圈遍歷行和列,列印交換輸入矩陣的首尾兩行後的結果矩陣

示例

下面的程式使用臨時變數和巢狀迴圈交換輸入矩陣的首尾兩行:−

# function to swap the first and last rows of an input matrix
# by accepting input matrix, rows, and columns as arguments
def swapFirstandLastRow(inputMatrix, rows, cols):
   # Swapping first and last row elements using temp variable
      for p in range(rows):
         temp = inputMatrix[0][p]
         inputMatrix[0][p] = inputMatrix[rows-1][p]
         inputMatrix[rows-1][p] = temp
# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
# calling the above-defined swapFirstandLastRow() function
# by passing input matrix, rows, and columns to it
swapFirstandLastRow(inputMatrix, rows, cols)
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
   for q in range(cols):
      #printing the corresponding element at the current row and column of the matrix
      print(inputMatrix[p][q], end=" ")
   # Printing a new line
   print()

輸出

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

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

方法2:使用交換(,)運算子(無需迴圈)

示例

下面的程式在不使用任何迴圈和(,)運算子的情況下交換輸入矩陣的首尾兩行:−

# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# Swapping first and last rows using the (,) operator
inputMatrix[0], inputMatrix[-1] = inputMatrix[-1], inputMatrix[0]
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
  for q in range(cols):
     # Printing the corresponding element at the current row & column
     print(inputMatrix[p][q], end=" ")
  print()

輸出

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

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

方法3:使用pop()、insert()和append()函式

演算法(步驟)

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

  • 使用負索引獲取矩陣的最後一行。

  • 使用正索引獲取矩陣的第一行。

  • 使用pop()函式(從列表中刪除最後一個元素並返回它)刪除矩陣的最後一行。

  • 透過將索引作為0傳遞給pop()函式來刪除矩陣的第一行。

  • 使用insert()函式(在指定的索引處插入值)在索引0(第一行)處插入最後一行。

  • 使用append()函式(在末尾將元素新增到列表)將矩陣的第一行附加到末尾。

  • 使用巢狀for迴圈遍歷行和列,列印交換輸入矩陣的首尾兩行後的結果矩陣

示例

下面的程式使用pop()、insert()和append()函式交換輸入矩陣的首尾兩行:−

# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# Getting the last row of a matrix
last_row = inputMatrix[-1]
# Getting the first row of a matrix
first_row = inputMatrix[0]
# Removing the last row of a matrix
inputMatrix.pop()
# Removing the first row of a matrix
inputMatrix.pop(0)
# Inserting the last row at the index 0(first row)
inputMatrix.insert(0, last_row)
# Appending(Adding at the end) the first row of a matrix at the end
inputMatrix.append(first_row)
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
   for q in range(cols):
      #printing the corresponding element at the current row and column of the matrix
      print(inputMatrix[p][q], end=" ")
   print()

輸出

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

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

結論

在這篇文章中,我們學習瞭如何使用三種不同的方法交換矩陣的首尾元素:使用臨時變數、(,)運算子以及pop()和insert()等方法。我們還學習瞭如何使用pop()和insert()函式刪除和新增(插入)矩陣的行。我們學習瞭如何使用(,)運算子交換兩個變數/可迭代物件。

更新於:2023年1月27日

1K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.