Python程式:字典旋轉K位
Python 中實現的一種資料結構,通常稱為關聯陣列,就是字典。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。
給定一個包含一些隨機鍵值對的字典,在本文中,我們將學習如何在 Python 中將給定的字典旋轉 K 位。
使用的方法
以下是完成此任務的各種方法
使用列表推導式、items() 和字典推導式
使用字典推導式、deque.rotate() 和 items() 函式
示例
假設我們已經獲取了一個**輸入字典和 K 次旋轉**。我們現在將輸入字典旋轉**K 次**,並在 K 次旋轉後列印結果字典。
輸入
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} k=3
輸出
{1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
在上面的示例中,K 值為 3。輸入字典在
第一次旋轉後:{5:1, 2: 5, 4: 6, 1: 3, 9: 4}
第二次旋轉後:{9:4, 5:1, 2: 5, 4: 6, 1: 3}
第三次旋轉後:{1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
方法 1 使用列表推導式、items() 和字典推導式
在這種方法中,我們將瞭解如何使用列表推導式和字典推導式將字典旋轉 K 位。
語法
enumerate(iterable, start=0)
enumerate() 方法為可迭代物件新增一個計數器,並返回 enumerate 物件。
引數
iterable - 可以是任何支援迭代的序列/物件/可迭代物件
start - enumerate() 從此值開始計數。如果未指定 start,則使用值 0。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存**輸入字典**。
列印輸入字典。
建立另一個變數來儲存**輸入的 K 次旋轉數**。
使用**items()** 函式(返回一個檢視物件,即包含字典的鍵值對,作為列表中的元組)獲取字典的鍵和值。
使用**list()** 函式(將序列/可迭代物件轉換為列表)將其轉換為元組列表。
使用 enumerate() 函式遍歷列表的索引和元素,並使用數學邏輯將其旋轉 K 位。
使用字典推導式將結果元組列表再次轉換為字典。
列印旋轉字典 K 次後的結果字典。
示例
以下程式使用列表推導式、items() 和字典推導式返回在給定輸入 K 次旋轉後旋轉輸入字典後的字典。
# input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} # printing input dictionary print("Input Dictionary:", inputDict) # input K rotations k = 3 # getting the key, and value of the dictionary as a tuple and converting it into the list inputDict = list(inputDict.items()) # rotationg the input dictionary by k rotations resultList = [inputDict[(p - k) % len(inputDict)] for p, m in enumerate(inputDict)] # converting the result list of tuples into a dictionary again # using dictionary comprehension resultantDict = {e[0]: e[1] for e in resultList} # printing the resultant dictionary after given input k rotations print("Resultant dictionary after", k, "rotations:", resultantDict)
輸出
執行上述程式後,將生成以下輸出:
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
方法 2 使用字典推導式、deque.rotate() 和 items() 函式
雙端佇列,也稱為**deque**,允許使用者在任一端新增和刪除專案。Deque 模組屬於 collections 庫。它提供了可以直接與引數一起使用的新增和刪除專案的方法。
當我們需要從容器的兩端進行更快的追加和彈出操作時,會選擇 deque 而不是列表,因為 deque 為追加和彈出操作提供了**O(1)** 的時間複雜度,而列表提供了 O(n) 的時間複雜度。
**deque.rotate() 函式** - 使用此函式根據引數中給定的數字旋轉 deque。如果給定的數字為負數,則旋轉將向左進行。否則,旋轉將向右進行。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
使用 import 關鍵字從 collections 模組匯入 deque。
使用 items() 函式獲取字典的鍵值對。
使用 list() 函式(將序列/可迭代物件轉換為列表)將其轉換為元組列表。
透過將輸入字典作為引數傳遞給 deque() 函式,將其轉換為 deque。
透過將輸入 K 值作為引數傳遞給 rotate() 函式,並將其應用於 deque,從而將輸入字典旋轉 K 次。
使用 list() 函式(返回可迭代物件的列表)將結果轉換為列表。
使用字典推導式將結果元組列表再次轉換為字典。
列印旋轉字典 K 次後的結果字典。
示例
以下程式使用字典推導式、deque.rotate() 和 items() 函式返回在給定輸入 K 次旋轉後旋轉輸入字典後的字典。
# importing deque from the collections module from collections import deque # input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} # printing input dictionary print("Input Dictionary:", inputDict) # input K rotations k = 3 # getting the key, value of dictionary as a tuple # and converting it into the list inputDict = list(inputDict.items()) # Converting input dictionary to deque dequeObject = deque(inputDict) # rotating the deque object by k rotations dequeObject.rotate(k) # converting into the list resultList = list(dequeObject) # converting the result list of tuples into a dictionary again resultantDict = {e[0]: e[1] for e in resultList} # printing the resultant dictionary after input k rotations print("Resultant dictionary after", k, "rotations:", resultantDict)
輸出
執行上述程式後,將生成以下輸出:
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
結論
在本文中,我們學習了兩種不同的方法來將字典旋轉 K 次。我們還學習瞭如何使用 rotate() 函式向 deque 物件新增旋轉,以及如何將給定字典轉換為 deque。