Python 交换矩阵中第一行和最后一行的元素

Python 交换矩阵中第一行和最后一行的元素

在本文中,我们将学习一个Python程序,用于在矩阵中交换第一行和最后一行的元素。

使用的方法

  • 使用临时变量(使用嵌套循环)

  • 使用交换(“,”)操作符(不使用任何循环)

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

方法1:使用临时变量(使用嵌套循环)

步骤

以下是执行所需任务的算法/步骤。

  • 创建一个函数 swapFirstandLastRow() ,通过接受输入矩阵、行和列作为参数来交换输入矩阵的第一行和最后一行。

  • 遍历矩阵的行和列,并使用临时变量交换第一行和最后一行的元素。

  • 创建一个变量来存储输入矩阵。

创建一个变量来存储矩阵的输入行数。

  • 创建另一个变量来存储矩阵的输入列数。

  • 通过将输入矩阵、行和列传递给上述定义的 swapFirstandLastRow() 函数来调用它以交换第一行和最后一行。

  • 通过使用嵌套循环遍历行和列,打印在交换输入矩阵的第一行和最后一行后得到的结果矩阵。

示例

以下程序使用临时变量和嵌套循环交换输入矩阵的第一行和最后一行。

# 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:使用Swapping(“,”)操作符(不使用任何循环)

示例

下面的程序在不使用任何循环和(,)操作符的情况下交换输入矩阵的第一行和最后一行。

# 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() 函数(从列表中删除最后一个元素并返回)来删除矩阵的最后一行。

  • 使用 pop() 函数传递索引0来删除矩阵的第一行。

  • 使用 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()函数来删除和添加(插入)矩阵的行。我们学习了如何使用(,)操作符来交换两个变量/可迭代对象。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程