Python程序:反转矩阵中每第K行


在Python中,矩阵是一种特殊的二维数组,其中所有数据元素的大小必须完全相同。因此,每个矩阵也是一个二维数组,但反过来不一定成立。对于许多数学和科学任务,矩阵是必不可少的数据结构。

在本文中,我们将学习一个Python程序,用于反转矩阵中每第K行。

使用方法

以下是完成此任务的各种方法:

  • 使用for循环和reversed()函数

  • 使用列表推导和切片

  • 使用index()和reverse()函数

  • 使用range()函数

示例

假设我们已经输入了一个列表和第K行号。我们现在将使用上述方法反转输入第K行的元素,并打印结果矩阵。

输入

inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
kth_rowno = 2

输出

[[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

在这个例子中,输入第K行=2。因此,输入矩阵的第2行的元素被反转,并打印结果矩阵。

方法1:使用for循环和reversed()函数

在这个函数中,我们将借助Python的for循环和reversed()函数来执行给定的任务。

语法

enumerate(iterable, start=0)

enumerate()函数为迭代器添加计数器,并返回enumerate对象。

算法(步骤)

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

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

  • 打印输入矩阵。

  • 创建一个另一个变量来存储要反转的输入第K行

  • 创建一个空列表用于存储结果矩阵。

  • 使用for循环遍历输入矩阵的索引和元素,并使用enumerate()函数。

  • 使用if条件语句检查索引是否为k的倍数。

  • 使用reversed()函数反转当前元素,并使用list()函数将其转换为列表。

  • 使用append()函数将该元素添加到结果列表中。

  • 否则,在不进行任何修改的情况下追加当前元素。

  • 反转输入矩阵的输入第K行后,打印结果矩阵。

示例

下面的程序使用for循环和reversed()函数返回反转输入矩阵的输入第K行后的矩阵:

# input matrix
inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
# printing input matrix
print("Input Matrix:\n", inputMatrix)
# input Kth row
kth_rowno = 2
# resultant list for storing matrix
resultantList = []
# traversing through index, element of the input matrix
for index, element in enumerate(inputMatrix):
    # checking if the index is multiple of K
    if (index + 1) % kth_rowno == 0:
        # reversing the current element and converting it to a list and
        # then appending to the resultant list if the condition is true
        resultantList.append(list(reversed(element)))
    else:
        # else appending current element to resultant list without any modification
        resultantList.append(element)
# printing the resultant matrix after reversing the given Kth row
print("Matrix after reversing the elements of 2nd row:\n", resultantList)

输出

Input Matrix:
 [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
Matrix after reversing the elements of 2nd row:
 [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]] 

方法2:使用列表推导和切片

当您希望基于现有列表的值构建新列表时,列表推导提供了一种更短/简洁的语法。

示例

下面的程序使用列表推导和切片返回反转输入矩阵的输入第K行后的矩阵:

# input matrix
inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
# input Kth row
kth_rowno = 2
# performing in concise syntax using list comprehension
resultantList = [element[::-1] if (index + 1) % kth_rowno == 0 else element for index,
                 element in enumerate(inputMatrix)]
# printing the resultant matrix after reversing the given Kth row
print("Matrix after reversing the elements of 2nd row:\n", resultantList)

输出

Matrix after reversing the elements of 2nd row:
 [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

方法3:使用index()和reverse()函数

在这个方法中,我们将使用Python的index()和reverse()函数的组合来反转矩阵中的每第K行。这里,revers()函数就地反转列表对象,这意味着它不占用任何额外空间,只修改原始列表。

语法

list.index(element)

index函数返回提供的值的第一次出现的位置。

示例

下面的程序使用index()和reverse()函数返回反转输入矩阵的输入第K行后的矩阵:

# input matrix
inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]

kth_rowno = 2
# resultant list for storing matrix
resultantList = []
for element in inputMatrix:
    # checking whether the index of the current element is equal to the kth row-1(0 index)
    if(inputMatrix.index(element) == kth_rowno-1):
        # reversing that current element if the condition is true
        element.reverse()
        # appending that element to the resultant list
        resultantList.append(element)
    else:
        # else appending that current element to the resultant list without any modification
        resultantList.append(element)
# printing the resultant matrix after reversing the given Kth row
print("Matrix after reversing the elements of 2nd row:\n", resultantList)

输出

Matrix after reversing the elements of 2nd row:
 [[7, 1, 4], [6, 10, 3], [1, 4, 2], [8, 6, 1]]

方法4:使用range()函数

在这个方法中,我们将使用Python的range()函数来遍历矩阵中的每第K行。range()函数返回一个从0开始,每次递增1(默认),并在到达给定数字之前停止的数字序列。

示例

下面的程序使用range()函数返回反转输入矩阵的输入第K行后的矩阵:

# input matrix
inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
# input Kth row 
kth_rowno = 2
# traversing from kth_rowno-1 till the length with a step value as kth row
for index in range(kth_rowno-1, len(inputMatrix), kth_rowno):
  	# reversing current element, if the condition is true 
	inputMatrix[index] = inputMatrix[index][::-1]
# printing the resultant matrix after reversing the given Kth row
print("Matrix after reversing the elements of 2nd row:\n", inputMatrix)

输出

Matrix after reversing the elements of 2nd row:
 [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

结论

在本文中,我们介绍了如何反转矩阵中的每第K行。我们还学习了如何使用enumerate()函数遍历迭代器的索引和元素。

更新于:2023年8月18日

622 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.