Python 如何遍历字典中的元组
在本文中,我们将学习如何在Python中遍历字典中的元组。
使用的方法
以下是实现此任务的各种方法:
- 使用索引
-
使用dictionary.values()方法
-
使用dictionary.items()方法
Tuples 是一种不可变的、无序的数据类型,用于在Python中存储集合。列表和元组在许多方面相似,但列表具有可变长度,而元组具有固定长度且是不可变的。
方法1:使用索引
len()函数 返回对象中的项目数量。当对象是字符串时,len()函数返回字符串中的字符数。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储包含 元组 作为值的输入字典。
-
使用键打印与输入字典的特定键(此处为10)映射的整个元组。
-
使用 in 运算符遍历字典的元组元素,逐个打印与字典的键映射的元组的元素。
-
使用 range()函数 遍历字典的元组元素的长度,打印与字典的键映射的元组的元素。
示例
以下程序遍历字典中的元组,并使用索引打印每个元组的元素。
# input dictionary
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
print("The Tuple mapped with the key 10 of the dictionary is:"),
# printing the whole tuple mapped with the key 10 of the dictionary
print(inputDict[10])
print()
print("The Tuple mapped with the key 20 of the dictionary is:"),
# printing the elements of the tuple mapped with
# the key 20 of the dictionary one-by-one using for loop and in operator
for i in inputDict[20]:
# printing the corresponding element
print(i),
print()
print("The Tuple mapped with the key 30 of the dictionary is:"),
# printing the elements of the tuple mapped with
# the key 20 of the dictionary one-by-one using for loop and range()
for k in range(0, len(inputDict[30])):
# accessing the corresponding element
print(inputDict[30][k])
输出
执行以上程序后会生成以下输出−
The Tuple mapped with the key 10 of the dictionary is:
('Hello', 'Tutorialspoint', 'Python')
The Tuple mapped with the key 20 of the dictionary is:
dhoni
virat
pandya
rohit sharma
The Tuple mapped with the key 30 of the dictionary is:
this
is
a
dictionary
方法2:使用values()函数
使用dictionary.values()函数结合for循环来遍历字典中的元组。
values()函数 (dict.values()方法提供了一个视图对象,按照插入顺序显示字典中所有值的列表)
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储包含值的输入字典,每个值都是一个元组。
-
使用for循环通过values()函数遍历字典中的值(所有元组)。
-
再使用另一个嵌套的for循环来遍历每个元组中的元素。
-
打印元组的对应元素。
示例
以下程序遍历字典中的元组,并使用dictionary.values()函数打印每个元组的元素。
# input dictionary containing the values as a tuple
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
# traversing through the values(tuples) of a dictionary using values() function
for p in inputDict.values():
# traversing within each tuple again using another nested for loop
for q in p:
# printing the elements of the corresponding tuple one by one
print(q)
print(" ")
输出
执行以上程序将生成以下输出结果−
Hello
Tutorialspoint
Python
dhoni
virat
pandya
rohit sharma
this
is
a
dictionary
方法3:使用dictionary.items()方法
使用 dictionary.items() 函数在for循环中遍历字典中的元组。
items() 函数返回一个视图对象,即以列表中的元组形式包含字典的键值对。
步骤
以下是执行所需任务的算法/步骤。−
- 创建一个变量来存储包含值为 tuple 的输入字典。
-
使用 for循环 通过 items()函数 遍历字典的键和值(所有元组)。
-
使用另一个嵌套的for循环遍历元组的所有元素。
-
打印元组元素。
示例
以下程序使用dictionary.items()函数遍历字典中的元组,并打印每个元组的元素。−
# input dictionary containing the values as a tuple
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
# traversing through keys,values(tuples) of a dictionary using items() function
for key, tuplevalues in inputDict.items():
# traversing within values of each tuple again using another nested for loop
for value in tuplevalues:
# printing the elements of the corresponding tuple elements one by one
print(value)
print(" ")
输出
运行上述程序后,将生成以下输出:
Hello
Tutorialspoint
Python
dhoni
virat
pandya
rohit sharma
this
is
a
dictionary
结论
该文章为我们提供了三种不同的方法来迭代给定字典中的元组。我们学会了如何使用values()函数来获取字典的值。此外,我们还学会了如何使用items()方法遍历字典的键和值。