如何在 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() 函数返回一个可迭代的字典值列表。方法 values() 返回的一系列值可以使用 for 循环进行迭代,并且可以随时打印每个值。
示例
以下是使用 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
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

