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()函式遍歷迭代器的索引和元素。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP