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()函数的组合来反转矩阵中的每一行。在这里,reverse()函数会就地反转列表对象,这意味着它不会占用任何额外的空间,只会修改原始列表。
语法
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()函数来遍历矩阵中的每一行。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()函数遍历可迭代对象的索引和元素。