在本教程中,我們將討論如何在 Python 中將元組轉換為字典。
我們知道元組的元素包含在括弧中,字典的元素以鍵值對的形式存在,並包含在花括弧中。
我們將使用以下技術在 Python 中將元組轉換為字典-
- 使用 setdefault()
- 使用 dict()
- 使用字典推導
- 使用 zip()和 dict()
讓我們從第一個開始,
setdefault()的功能是返回與某個鍵相關聯的值,如果該鍵不存在,則會插入一個默認值。
下面的程序說明了如何在 Python 程序中使用它。
#creating a function
def convert_dict(tup, dic):
for i, j in tup:
dic.setdefault(i, []).append(j)
return dic
#initialising the tuple values
tuple_values = [("English", 2001), ("Hindi", 2002), ("Mathematics", 2003),
("Computer Science", 2004), ("Physics", 2005), ("Chemistry", 2006)]
res_dictionary = {}
#displaying the resultant dictionary
print ("The converted dictionary is: ", convert_dict(tuple_values,res_dictionary))
輸出:
The converted dictionary is: {'English': [2001], 'Hindi': [2002], 'Mathematics': [2003], 'Computer Science': [2004], 'Physics': [2005], 'Chemistry': [2006]}
解釋-
- 在第一步中,我們創建了一個以元組和字典為輸入的函數。
- 之後,我們使用
for
循環使用 setdefault() 並追加主題名稱和主題代碼。 - 現在我們已經初始化了元組的值,並將結果字典聲明為{}。
- 在執行上述程序時,會顯示預期的輸出。
在第二個程序中,我們將學習 dict()如何用於同樣的目的。
dict()用於在 Python 中創建字典,讓我們看看它如何為我們的程序增加意義。
考慮下面給出的程序,
#creating a function
def convert_dict(tup, dic):
#use of dict()
dic = dict(tup)
return dic
#initialising the tuple values
tuple_values = [("English", 2001), ("Hindi", 2002), ("Mathematics", 2003),
("Computer Science", 2004), ("Physics", 2005), ("Chemistry", 2006)]
res_dictionary = {}
#displaying the resultant dictionary
print ("The converted dictionary is: ", convert_dict(tuple_values,res_dictionary))
輸出:
The converted dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}
解釋-
- 在第一步中,我們創建了一個以元組和字典為輸入的函數。
- 在此之後,我們使用
for
循環來使用 dict(),它將元組作為參數並返回字典。 - 現在我們已經初始化了元組的值,並將結果字典聲明為{}。
- 在執行上述程序時,會顯示預期的輸出。
在第三個節目中,我們將看到字典推導如何幫助我們。
下面的程序顯示了同樣的情況,
#initialising the tuple values
sub_names = ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')
sub_codes = (2001, 2002, 2003, 2004, 2005, 2006)
#displaying the tuples
print("The values in sub_names are: ", sub_names)
print("The values in sub_codes are: ", sub_codes)
if len(sub_names) == len(sub_codes):
res_dict = {sub_names[i]: sub_codes[i] for i, _ in enumerate(sub_codes)}
#displaying the resultant dictionary
print("The resultant dictionary is: ", res_dict)
輸出:
The values in sub_names are: ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')
The values in sub_codes are: (2001, 2002, 2003, 2004, 2005, 2006)
The resultant dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}
解釋-
- 首先,我們初始化了兩個元組,子名稱和子代碼,並顯示它們。
- 此後,決策關鍵字 if 用於檢查兩個元組的長度是否相同,如果相同,則執行字典推導中定義的功能。
- 在執行給定程序時,會顯示所需的輸出。
在最後一個程序中,我們將學習如何在 Python 程序中使用 zip()和 dict()。
我們已經了解了 dict()是如何工作的,這裡我們將應用 dict()和 zip(),zip()方法獲取可迭代的項,並將它們附加到一起形成一個元組。
下面的程序說明了同樣的情況-
#initialising the tuple values
sub_names = ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')
sub_codes = (2001, 2002, 2003, 2004, 2005, 2006)
#displaying the tuples
print("The values in sub_names are: ", sub_names)
print("The values in sub_codes are: ", sub_codes)
if len(sub_names) == len(sub_codes):
#using zip() and dict()
res_dict = dict(zip(sub_names, sub_codes))
#displaying the resultant dictionary
print("The resultant dictionary is: ", res_dict)
輸出:
The values in sub_names are: ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')
The values in sub_codes are: (2001, 2002, 2003, 2004, 2005, 2006)
The resultant dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}
解釋-
- 首先,我們初始化了兩個元組,子名稱和子代碼,並顯示它們。
- 此後,決策關鍵字 if 用於檢查兩個元組的長度是否相同,如果相同,則執行涉及 zip()和 dict()的功能。
- 在執行給定程序時,會顯示所需的輸出。
在本教程中,我們學習了在 Python 中將元組轉換為字典的不同方法。
原創文章,作者:VI21P,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126693.html