Python 列表元素旋轉程式
在 Python 中,列表可以用來在一個變數中儲存多個專案。列表是 Python 用於儲存資料集合的四種內建資料型別之一。另外三種分別是元組、集合和字典,每種都具有不同的功能。列表使用方括號建立。由於列表不必是同構的,因此它們是 Python 中最靈活的工具。一個列表包含字串、物件和整數等資料型別。列表是可變的,這意味著在建立後可以修改它們。
本文重點介紹簡寫方法,以及用單行程式碼或一個單詞快速表達這些方法的多種方法。對於程式設計師來說,執行許多工時,此操作非常重要。我們將看到使用 python 完成此任務的四種不同方法。
使用列表推導式
使用此方法時,我們只需在特定位置旋轉後重新分配列表中每個元素的索引。由於實現更簡潔,此方法在完成任務方面發揮著重要作用。
演算法
首先定義一個列表。
使用列表推導式。
應用左右兩個方向的旋轉(i-index 和 i+index)。
列印輸出列表。
語法
# 左旋轉
list_1 = [list_1[(i + 3) % len(list_1)]
# 右旋轉
list_1 = [list_1[(i - 3) % len(list_1)]
示例
在此程式碼中,我們使用了列表推導式來旋轉列表中的元素,即右旋轉和左旋轉。for 迴圈用於迭代列表中的元素。
list_1 = [10, 14, 26, 37, 42] print (" Primary list : " + str(list_1)) list_1 = [list_1[(i + 3) % len(list_1)] for i, x in enumerate(list_1)] print ("Output of the list after left rotate by 3 : " + str(list_1)) list_1 = [list_1[(i - 3) % len(list_1)] for i, x in enumerate(list_1)] print ("Output of the list after right rotate by 3(back to primary list) : "+str(list_1)) list_1 = [list_1[(i + 2) % len(list_1)] for i, x in enumerate(list_1)] print ("Output of the list after left rotate by 2 : " + str(list_1)) list_1 = [list_1[(i - 2) % len(list_1)] for i, x in enumerate(list_1)] print ("Output of the list after right rotate by 2 : "+ str(list_1))
輸出
Primary list : [10, 14, 26, 37, 42] Output of the list after left rotate by 3 : [37, 42, 10, 14, 26] Output of the list after right rotate by 3(back to primary list) : [10, 14, 26, 37, 42] Output of the list after left rotate by 2 : [26, 37, 42, 10, 14] Output of the list after right rotate by 2 : [10, 14, 26, 37, 42]
在此程式碼中,我們使用了列表推導式來旋轉列表中的元素,即右旋轉和左旋轉。for 迴圈用於迭代列表中的元素。
使用切片
此特定技術是標準技術。它只是將後面切片的部分與前面切片的部分連線起來,並使用旋轉次數。
演算法
首先定義一個列表。
使用切片方法。
列印右旋轉和左旋轉後的每個列表。
語法
切片
# 左旋轉 –
list_1 = list_1[3:] + list_1[:3]
# 右旋轉 −
list_1 = list_1[-3:] + list_1[:-3]
示例
以下程式重新排列列表中的元素。原始列表為 [11, 34, 26, 57, 92]。首先向左旋轉 3 個單位。也就是說,前三個元素被移到末尾,結果為 [57, 92, 11, 34, 26]。然後向右旋轉 3 個單位,以便最後三個元素移回其原始位置 [11,34,26,57,92]。
然後向右旋轉 2 個單位,以便最後兩個元素向前移動,得到 [26, 57, 92 11 34]。最後向左旋轉 1 個單位,將一個元素從開頭移動到末尾,得到 [57 92 11 34 26]。
list_1 = [11, 34, 26, 57, 92] print (" Primary list : " + str(list_1)) list_1 = list_1[3:] + list_1[:3] print ("Output of the list after left rotate by 3 : " + str(list_1)) list_1 = list_1[-3:] + list_1[:-3] print ("Output of the list after right rotate by 3(back to Primary list) : "+str(list_1)) list_1 = list_1[-2:] + list_1[:-2] print ("Output of the list after right rotate by 2 : "+ str(list_1)) list_1 = list_1[1:] + list_1[:1] print ("Output of the list after left rotate by 1 : " + str(list_1))
輸出
Primary list : [11, 34, 26, 57, 92] Output of the list after left rotate by 3 : [57, 92, 11, 34, 26] Output of the list after right rotate by 3(back to Primary list) : [11, 34, 26, 57, 92] Output of the list after right rotate by 2 : [57, 92, 11, 34, 26] Output of the list after left rotate by 1 : [92, 11, 34, 26, 57]
使用 NumPy 模組
為了旋轉列表中的元素,我們還可以使用 python 中的 numpy.roll 模組以及給定的軸。這實際上會將輸入陣列中的元素移位。如果元素從第一個位置移動到最後一個位置,則它將被反轉到初始位置。
演算法
匯入 numpy.roll 模組
定義列表並給出特定的索引。
列印輸出列表。
示例
建立一個列表“number”,併為其分配值 1、2、4、10、18 和 83。變數 i 設定為 1。然後,NumPy 庫中的 np.roll() 函式用於列表 number,其引數為 i,這會將列表中每個元素的索引位置移位 1(第一個元素變為最後一個)。
import numpy as np if __name__ == '__main__': number = [1, 2, 4, 10, 18, 83] i = 1 x = np.roll(number, i) print(x)
輸出
[83 1 2 4 10 18]
使用 collections.deque.rotate()
rotate() 函式是 collections 模組中 deque 類提供的內建函式,用於執行旋轉。儘管不太為人所知,但此函式非常有用。
演算法
首先從 collection 模組匯入 deque 類。
定義一個列表
列印主列表
使用 rotate() 旋轉元素
列印輸出。
示例
以下程式使用 collections 模組中的 deque 資料結構來旋轉列表。列印原始列表,然後向左旋轉 3 個單位並列印新的旋轉列表。然後向右旋轉(回到其原始位置)3 個單位並列印結果列表。
from collections import deque list_1 = [31, 84, 76, 97, 82] print ("Primary list : " + str(list_1)) list_1 = deque(list_1) list_1.rotate(-3) list_1 = list(list_1) print ("Output list after left rotate by 3 : " + str(list_1)) list_1 = deque(list_1) list_1.rotate(3) list_1 = list(list_1) print ("Output list after right rotate by 3(back to primary list) : "+ str(list_1))
輸出
Primary list : [31, 84, 76, 97, 82] Output list after left rotate by 3 : [97, 82, 31, 84, 76] Output list after right rotate by 3(back to primary list) : [31, 84, 76, 97, 82]
結論
在本文中,我們簡要解釋了旋轉列表中元素的四種不同方法。