使用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.