如何将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。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP