如何在Python中列印字典的所有值?
Python 內建了一個名為 `values()` 的方法,它返回一個檢視物件。字典的值列在這個檢視物件中。
我們可以使用 Python 中的 `values()` 方法從字典中檢索所有值。Python 的內建 `values()` 方法返回一個檢視物件,該物件表示一個包含每個值的字典列表。
本文解釋了在 Python 中列印字典的所有值的各種方法。
使用 `dict.values()` 方法
Python 的`dict.values()` 方法可以用來檢索字典的值,然後可以使用 `print()` 函式列印這些值。
示例
下面是一個使用 `dict.values()` 方法列印字典所有值的示例:
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } print(dictionary.values())
輸出
以下是上述程式碼的輸出:
dict_values['Pride and Prejudice', '1813', 'Jane Austen', 'Elizabeth Bennet']
使用 for 迴圈
Python 字典類中的 `dict.values()` 函式返回一個可迭代的字典值列表。可以使用for 迴圈迭代 `values()` 方法返回的值序列,並在迭代過程中列印每個值。
示例
下面是一個使用 for 迴圈列印字典所有值的示例:
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Iterating over all the values of a dictionary and printing them one by one for values in dictionary.values(): print(values)
輸出
以下是上述程式碼的輸出:
Pride and Prejudice
1813
Jane AustenElizabeth Bennet
透過建立所有值的列表
`dict.values()` 函式返回的可迭代序列也可以用來建立一個值列表。然後可以列印列表的全部內容(字典的所有值)。
示例
下面是一個透過建立所有值的列表來列印字典所有值的示例:
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Getting all the values of a dictionary as a list list_of_the_values = list(dictionary.values()) # Printing the list which contains all the values of the dictionary print(list_of_the_values)
輸出
以下是上述程式碼的輸出
['Pride and Prejudice', '1813', 'Jane Austen', 'Elizabeth Bennet']
透過建立列表推導式
我們可以使用此列表推導式遍歷字典的所有內容,然後一次一個地顯示每個值。
示例
下面是一個透過建立列表推導式來列印字典所有值的示例:
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Iterating over all the values of the dictionary and printing them one by one [print(values) for values in dictionary.values()]
輸出
以下是上述程式碼的輸出:
Pride and Prejudice 1813 Jane Austen Elizabeth Bennet
列印巢狀字典的所有值
考慮一下我們有巢狀字典的情況,這是一種使用其他字典作為值欄位的字典類。我們可以使用遞迴來遍歷巢狀字典中的每個值。
我們開發了一個方法,可以返回給定字典中的所有值。遍歷字典中的所有鍵值對,它會確定每對的值是否是字典型別,如果值不是字典型別,它會生成該值。
當值是字典型別時,此函式將呼叫自身以訪問巢狀字典中的每個值並返回所有值。該過程將持續進行,直到所有巢狀字典都被遍歷。
示例
下面是一個列印巢狀字典所有值的示例:
# Nested Dictionary Novels = { 'Novel 1': {'Name': 'Pride and Prejudice', 'year': 1813, 'author': 'Jane Austen'}, 'Novel 2': {'Name': 'Listen to Your Heart: The London Adventure', 'year': 2022, 'author': 'Ruskin Bond'}, 'Novel 3': {'Name': 'A Place Called Home', 'year': 2021, 'author': 'Preeti Shenoy'}, 'Novel 4': {'Name': 'Leaders, Politicians, Citizens', 'year': 2020, 'author': ' Rasheed Kidwai '}, } def all_the_values(dictionary): # Iterating over all the values of the dictionary for keys , values in dictionary.items() # If the values are of dictionary type then yield all the values in the nested dictionary if isinstance(values, dict): for x in all_the_values(values): yield x else: yield values # Iterating over all the values of a nested dictionary and printing them one by one. for values in all_the_values(Novels): print(values)
輸出
以下是上述程式碼的輸出。
Pride and Prejudice 1813 Jane Austen Listen to Your Heart: The London Adventure 2022 Ruskin Bond A Place Called Home 2021 Preeti Shenoy Leaders, Politicians, Citizens 2020 Rasheed Kidwai