如何在 Python 中迭代字典中的元组


在这篇文章中,我们将学习如何在 Python 中迭代字典中的元组。

使用的方法

以下是完成此任务的各种方法:

  • 使用索引

  • 使用 dictionary.values() 方法

  • 使用 dictionary.items() 方法

元组是 Python 中用于存储集合的不可变、无序数据类型。列表和元组在许多方面相似,但列表的长度可变且是可变的,而元组的长度固定且是不可变的

方法 1:使用索引

len() 函数 - 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()函数返回一个视图对象,即它包含字典的键值对,作为列表中的元组。

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储包含值为元组的输入字典。

  • 使用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() 方法遍历字典的键和值。

更新于:2023年1月27日

3K+ 次浏览

启动您的职业生涯

完成课程后获得认证

开始
广告
© . All rights reserved.