如何將Python元組轉換為字典?
Python元組的元素用括號括起來,而字典的元素以鍵值對的形式出現,並用花括號括起來。
在本文中,我們將向您展示如何將Python元組轉換為字典。以下是將元組轉換為字典的方法:
使用dict()函式
使用字典推導式和enumerate()函式
使用zip()和dict()函式
假設我們已經獲取了一個包含一些元素的元組。我們將使用上面指定的不同方法將輸入元組轉換為Python字典並返回它。
方法1:使用dict()函式
在Python中,使用dict()函式可以將元組轉換為字典。dict()函式可以建立一個字典物件。dict()方法返回字典,它接受一個元組的元組作為引數。每個元組包含一個鍵值對。
在下面的程式碼中,為了建立一個字典,我們使用了dict()方法,並將字典推導式作為引數。字典推導式是一種將一個字典轉換為另一個字典的技術。在這個轉換過程中,可以根據需要有條件地將原始字典中的元素包含在新字典中,並且可以根據需要轉換每個元素。
輸出是一個鍵值對字典。元組的第一個元素成為字典鍵,而元組的第二個元素成為字典值。
示例
下面的程式使用dict()函式將元組轉換為字典:
# input tuple inputTuple = ((5, "TutorialsPoint"), (6, "Python"), (7, "Codes")) print("The input Tuple:", inputTuple) # Here we are iterating through each element (pairs) of the tuple using dictionary comprehension and converting it to the dictionary resultDictionary = dict((x, y) for x, y in inputTuple) print("The result dictionary:", resultDictionary)
輸出
執行上述程式將生成以下輸出:
The input Tuple: ((5, 'TutorialsPoint'), (6, 'Python'), (7, 'Codes')) The result dictionary: {5: 'TutorialsPoint', 6: 'Python', 7: 'Codes'}
方法2:使用字典推導式和enumerate()函式
注意 - 要將兩個元組轉換為字典,這兩個元組的長度必須相同。否則,我們將無法匹配所有鍵值對。
演算法(步驟)
以下是執行所需任務的演算法/步驟
建立兩個變數來儲存長度相同的第一個和第二個輸入元組。
使用if條件語句檢查兩個元組的長度是否相等。
如果條件為真,則使用 enumerate() 函式在字典推導式中將兩個元組轉換為字典。
轉換後,列印來自給定兩個元組的結果字典。
enumerate()方法向可迭代物件新增一個計數器,並返回enumerate物件。
語法
enumerate(iterable, start=0)
引數
iterable - 它可以是任何支援迭代的序列/物件/可迭代物件
start - enumerate() 從此值開始計數。如果未指定start,則使用值0。
示例
下面的程式使用字典推導式方法(一個元組作為鍵,另一個元組作為字典的值)將兩個元組轉換為字典:
# input tuple_1 inputTuple_1 = ('TutorialsPoint', 'Python', 'Codes') # input tuple_2 inputTuple_2 = (5, 6, 7) # printing the input tuple_1(keys) print("The input Tuple_1(keys) = ", inputTuple_1) # printing the input tuple_2(values) print("The input Tuple_2(values) = ", inputTuple_2) # Checking whether the length of both the tuples are equal or not if len(inputTuple_1) == len(inputTuple_2): # converting both the tuples into a dictionary using enumerate() # function in a dictionary comprehension resultDictionary = {inputTuple_1[i] : inputTuple_2[i] for i, _ in enumerate(inputTuple_2)} # printing the result dictionary from the given two tuples print("The result dictionary:", resultDictionary)
輸出
The input Tuple_1(keys) = ('TutorialsPoint', 'Python', 'Codes') The input Tuple_2(values) = (5, 6, 7) The result dictionary: {'TutorialsPoint': 5, 'Python': 6, 'Codes': 7}
方法3:使用zip()和dict()函式
演算法(步驟)
建立兩個變數來儲存長度相同的第一個和第二個輸入元組。
使用if條件語句檢查兩個元組的長度是否相等。
如果條件為真,則使用zip()和dict() 函式將兩個元組轉換為字典。
zip()方法 - zip()方法接受可迭代物件(可能為零個或多個),將它們組合成一個元組並返回它。
Syntax- zip(*iterables)
dict()函式 - dict()函式用於建立字典。
轉換後,列印來自給定兩個元組的結果字典。
示例
下面的程式使用zip()和dict()函式(一個元組作為鍵,另一個元組作為字典的值)將兩個元組轉換為字典:
# input tuple_1 inputTuple_1 = ('TutorialsPoint', 'Python', 'Codes') # input tuple_2 inputTuple_2 = (5, 6, 7) # printing the input tuple_1(keys) print("The input Tuple_1(keys) = ", inputTuple_1) # printing the input tuple_2(values) print("The input Tuple_2(values) = ", inputTuple_2) # Checking whether the length of both the tuples are equal or not if len(inputTuple_1) == len(inputTuple_2): # converting both the tuples into a dictionary using zip() # and dict() functions # Here zip function takes elements of input tuple 1 as keys and input tuple 2 elements as values # Then we convert this to a dictionary using dict() resultDictionary = dict(zip(inputTuple_1, inputTuple_2)) # printing result dictionary from the given two tuples print("The result dictionary:", resultDictionary)
輸出
The input Tuple_1(keys) = ('TutorialsPoint', 'Python', 'Codes') The input Tuple_2(values) = (5, 6, 7) The result dictionary: {'TutorialsPoint': 5, 'Python': 6, 'Codes': 7}
結論
在本文中,我們學習瞭如何使用三個不同的函式將元組轉換為字典。我們還觀察到,如果有兩個元組,我們可以使用兩種不同的方法zip()和enumerate()來建立字典,將第一個元組元素作為鍵,第二個元組元素作為值。
透過我們最新的Python 教程學習Python。