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循环
The dict.values()函数在Python的字典类中返回一个可迭代的字典值列表。方法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