如何在 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+ 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告