如何在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

更新于:2023年8月23日

9万+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.