如何从 Python 字典中获取所有值的列表?
在本文中,我们将向您展示如何使用几种方法从 Python 字典中提取所有值的列表。使用以下方法,我们可以从 Python 字典中获取所有值的列表 -
使用 dict.values() 和 list() 函数
使用 [] 和 *
使用列表推导式
使用 append() 函数和 For 循环
假设我们已经采用了一个示例字典。我们将使用如上所述的不同方法从 Python 字典中返回所有值的列表。
方法 1:使用 dict.values() 和list() 函数
要获取列表,我们可以结合使用 dictionary.values() 和 list() 函数。 values() 方法用于从键:值对访问值,然后使用 list() 函数将值转换为列表。
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
创建一个变量来存储输入字典
使用 values() 函数打印字典的所有值的列表(dict.values() 方法提供了一个视图对象,按插入顺序显示字典中所有值的列表),将其应用于输入字典并使用 list() 函数将结果转换为列表(将序列/可迭代对象转换为列表)。
示例
以下程序使用 len() 函数返回列表的大小 -
# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of values of a dictionary using values() function print(list(demoDictionary.values()))
输出
执行时,上述程序将生成以下输出 -
['TutorialsPoint', 'Python', 'Codes']
方法 2:使用 [ ] 和 *
我们可以使用 [] 和 * 获取字典值的完整列表。这里,values() 是一个字典方法,用于从字典中的键:值对中检索值,而 * 用于仅获取值而不是字典值,然后我们使用 list() 函数进入列表。
算法(步骤)
以下是执行所需任务要遵循的算法/步骤 -
创建一个变量来存储输入字典。
使用 values() 函数打印字典的所有值的列表(values() 方法返回一个视图对象。字典值作为列表存储在视图对象中)、[]、* 运算符。
示例
以下程序使用 [] 和 * 运算符返回字典的所有值的列表
# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of values of a dictionary with values() function # and * operator print([*demoDictionary.values()])
输出
执行时,上述程序将生成以下输出 -
['TutorialsPoint', 'Python', 'Codes']
方法 3:使用列表推导式
为了查找值列表,此方法使用列表推导式技术。它接受一个键作为输入,并使用 for 循环返回一个列表,其中包含列表中每个字典中键每次出现的对应值。与其他方法相比,它更优雅、更符合 Python 风格。
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
创建一个变量来存储输入字典
通过遍历字典的每个值,使用列表推导获取字典的所有值列表。
demoDictionary[dict_key] 表示字典值
打印输入字典的所有值的列表。
示例
以下程序使用列表推导方法返回字典的所有值的列表 -
# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Getting the list of values of the dictionary with the list comprehension # demoDictionary[dict_key] represents dictionary value dict_valuesList = [demoDictionary[dict_key] for dict_key in demoDictionary] # Printing the list of all the values of a dictionary print(dict_valuesList)
输出
执行时,上述程序将生成以下输出 -
['TutorialsPoint', 'Python', 'Codes']
方法 4:使用 append() 函数 & For 循环
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
创建一个变量来存储输入字典。
创建一个空列表来存储输入字典的所有键。
使用 for 循环,使用 values() 函数遍历字典的所有值(values() 方法返回一个视图对象。字典值以列表形式存储在视图对象中)。
使用 append() 函数将字典的每个值附加到列表中(将元素添加到列表末尾),方法是将相应的值作为参数传递给它。
打印字典中的所有值列表字典。
示例
以下程序使用 append() 函数和 For 循环返回字典中所有值的列表 -
# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # an empty list for storing dictionary values dictValuesList = [] # Traversing through all the values of the dictionary for dictValue in demoDictionary.values(): # appending each value to the list dictValuesList.append(dictValue) # Printing the list of values of a dictionary print(dictValuesList)
输出
执行时,上述程序将生成以下输出 -
['TutorialsPoint', 'Python', 'Codes']
我们使用一个空列表来存储字典的值,然后迭代这些值,使用 append() 函数将它们附加到列表中,并显示它们。
结论
本文教我们如何使用 values() 函数获取整个字典的值以及如何使用 list() 函数将字典的值转换为列表。此外,我们还学习了如何在同一代码中使用列表推导和 for 循环将 values() 方法返回的字典中的值转换为列表。最后,我们学习了如何使用 append() 函数将元素添加到列表中(这里我们将值添加到列表中)。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

