Python 如何将字典转换为列表

Python 如何将字典转换为列表

在本文中,我们将向您展示如何将Python字典转换为列表。以下是实现此任务的方法:

  • 使用list()函数和items()方法

  • 使用keys()方法

  • 使用values()方法

  • 使用列表推导式

  • 使用Zip()函数

  • 使用map()函数

  • 使用for循环和items()方法

字典 是Python的关联数组数据结构。字典是一组键值对的集合。每个键值对由一个键和其相关联的值表示。

字典由用大括号括起来的 键值对 列表定义,各键值对之间用逗号分隔。每个键的值用 冒号(:) 分隔。

字典不能单纯排序以获取排序后的表示。字典的定义是无序的,而其他类型(如列表和元组)不是。因此,您需要一个有序的数据类型,即列表,很可能是一个元组列表。

Python的字典类为此目的提供了三种方法。 items()方法、 keys()方法和 values()方法分别返回包含键值对元组、仅包含键和仅包含值的视图对象。内置的list方法将这些视图对象转换为列表对象。

使用list()函数和items()方法

Python的字典类提供了 items() 函数,它返回字典中所有键值对(字典项)的可迭代序列。这个返回的序列表示字典中实际的键值对。我们可以使用list()函数从这个可迭代序列中获取一个元组列表。

步骤

执行所需任务的算法/步骤如下:

  • 创建一个变量来存储输入的字典。

  • 使用 items() 函数(返回字典中的键值对组)获取字典的所有键值对,并使用 list() 函数(返回可迭代对象的列表)将字典项(键值对)转换为一个元组列表。

  • 打印转换后的字典的结果列表。

示例

以下程序使用list()函数和items()方法将Python字典转换为列表:

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# converting input dictionary items(key-value pair)
# to a list of tuples
resultList = list(inputDictionary.items())
# printing the resultant list of a dictionary
print(resultList)

输出

在执行时,上述程序将生成以下输出-

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用keys()方法

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入的字典。

  • 使用keys()函数打印字典的所有键的列表(keys()方法提供了一个以插入顺序显示字典中所有键的列表的视图对象),并使用list()函数将结果转换为列表(将序列/可迭代对象转换为列表)。

示例

以下程序使用list()和keys()函数返回字典的所有键的列表:

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# converting input dictionary keys to a list
resultList = list(inputDictionary.keys())
# printing the resultant list of a dictionary keys
print(resultList)

输出

在执行上述程序时,将生成以下输出结果−

['Hello', 'Tutorialspoint', 'python']

使用values()方法

要获取值列表,使用字典的values()方法。我们可以通过使用list()函数将值转换为列表。

示例

以下程序使用list()和values()函数返回字典的所有值的列表。

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# converting input dictionary values to a list
resultList = list(inputDictionary.values())
# printing the resultant list of a dictionary values
print(resultList)

输出

执行上述程序后,将生成以下输出结果 –

[10, 20, 30]

使用列表推导式

列表推导式使得在Python中创建列表变得简单。此外,你可以在同一行上改变元素的值。

使用 dictionary.items() 来检索项目的键和值,并将它们添加到列表中。

示例

以下程序使用列表推导式将Python字典转换为列表-

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# Storing key and value as list of tuples using list comprehension
resultList = [(key, value) for key, value in inputDictionary.items()]
# printing the resultant list of a dictionary key-values
print(resultList)

输出

在执行时,上述程序将生成以下输出:

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用Zip()函数

zip()函数可以用于组合两个列表/迭代器。通过这种方式,我们可以组合字典的.keys()和.values()方法以同时访问两个值。

示例

以下程序使用list zip()函数将Python字典转换为列表 –

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
#combining keys and values of dictionary using zip() function
resultList = zip(inputDictionary.keys(), inputDictionary.values())
# converting zip object to list
resultList = list(resultList)
# printing the resultant list of a dictionary key-values
print(resultList)

输出

执行上述程序后,将生成以下输出结果:

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用map()函数

map() 是一个内建函数,它允许您在一个迭代器/列表上映射任何函数。要创建列表的列表,请将list()函数映射到 dictionary.items() 的所有元素。

示例

以下程序使用list map()函数将Python字典转换为列表:

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# conveting list of keys and values of dictionary to a list using map() function
resultList = new_list = list(map(list, inputDictionary.items()))
# printing the resultant list of a dictionary key-values
print(resultList)

输出

执行上面的程序后,将产生以下输出结果:

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

使用for循环和items()方法

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入的字典。

  • 创建一个空列表,该列表给出了字典的键和值的结果列表。

  • 使用for循环遍历字典的每个键值对,使用items()函数(返回字典中键值对的组合)。

  • 使用append()函数(将元素添加到列表末尾)将对应的键值对列表添加到列表中。

  • 打印字典键值的结果列表。

示例

以下程序使用for循环、append()和items()函数将Python字典转换为列表。

# input dictionary
inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30}
# creating an empty list
resultList = []
# traversing through each key value pair of a dictionary using items() function
for key, val in inputDictionary.items():
   # appending list of corresponding key-value pair to a list
   resultList.append([key, val])
# printing the resultant list of a dictionary key-values
print(resultList)

输出

在执行上述程序时,将生成以下输出 –

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

结论

在本文中,我们学习了如何使用七种不同的方法将给定的字典转换为列表。我们还学习了如何使用items()函数来遍历字典。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程