Python 列表转换为字典列表
Python 中的数据处理、组织或结构化有很多常见的任务,其中一项任务是将列表转换为字典列表。这是一个简单的过程,允许我们将特定的键与其对应的值关联起来;创建一个易于访问和/或操作的字典集合。
在继续讲解技术之前,让我们首先看一个输入和输出示例。
输入
test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age']
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
方法一:使用循环
在这种方法中,我们简单地获取一个包含交替名称和年龄的列表,并将其转换为字典列表,其中每个生成的字典列表都代表一个人的信息,并为“姓名”和“年龄”提供键。通过对原始列表进行循环迭代,然后根据提供的键对值进行分组。创建的字典被附加到列表“result”中。
示例
test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age'] result = [] for i in range(0, len(test_list), len(key_list)): temp_dict = {} for j, key in enumerate(key_list): temp_dict[key] = test_list[i + j] result.append(temp_dict) print(result)
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
方法二:使用带 zip() 的列表推导式
这是另一种实现目标的方法,它使用列表推导式将列表转换为字典列表。它以 key_list 的长度确定的块为单位迭代 test_list,从而创建字典,这些字典与来自 key_list 的元素及其来自 test_list 切片的对应元素配对。然后,它最终将它们附加到结果列表,这是一个字典列表。zip() 函数负责元素的配对。
示例
test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age'] result = [{key: value for key, value in zip(key_list, test_list[i:i+len(key_list)])} for i in range(0, len(test_list), len(key_list))] print(result)
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
方法三:使用带切片的列表推导式
此方法还使用列表推导式以 key_list 的长度为增量迭代 test_list(在代码开头创建)。在列表推导式中,我们有一个字典推导式,它通过使用索引将来自 key_list 的键与 test_list 中的值配对来创建字典。
示例
test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age'] result = [{key_list[j]: test_list[i+j] for j in range(len(key_list))} for i in range(0, len(test_list), len(key_list))] print(result)
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
方法四:使用 Itertools.islice() 和 Iter()
在 Python 中,通常使用 itertools 进行迭代。其函数之一 islice() 允许我们通过指定起始和结束索引来提取可迭代对象的特定部分。通过使用 iter(test_list),我们可以为 test_list 创建一个迭代器。然后,此迭代器可以与 islice() 一起使用,以从 test_list 中提取特定数量的元素。
zip 函数将切片元素与 key_list 组合。然后,使用字典推导式创建字典。需要注意的是,以下代码中的迭代次数由公式决定:length(test_list) 除以 length(key_list)。
示例
import itertools test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age'] iter_test = iter(test_list) result = [{key: value for key, value in zip(key_list, itertools.islice(iter_test, len(key_list)))} for _ in range(len(test_list) // len(key_list))] print(result)
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
方法五:使用生成器函数
生成器函数是一种可以生成一系列值并一次一个地生成它们的功能;允许高效的内存使用。yield 关键字用于生成值,并且可以使用简单的循环遍历该函数。以下方法定义了一个名为 generate_dicts 的生成器,它将 test_list 和 key_list 作为输入。字典是通过将来自 key_list 的元素与其在 test_list 中的对应元素配对来生成的。
示例
def generate_dicts(test_list, key_list): for i in range(0, len(test_list), len(key_list)): yield {key_list[j]: test_list[i+j] for j in # yield keyword instead of return range(len(key_list))} test_list = ['John', 25, 'Smith', 30, 'Alice', 35] key_list = ['name', 'age'] result = list(generate_dicts(test_list, key_list)) print(result)
输出
[{'name': 'John', 'age': 25}, {'name': 'Smith', 'age': 30}, {'name': 'Alice', 'age': 35}]
结论
在本文中,我们发现了在 Python 中将列表转换为字典列表的不同方法。这些方法包括使用循环;带 zip() 的列表推导式;带切片的列表推导式;itertools.islice() 和 iter(),最后是生成器函数。所有这些方法都为我们提供了将数据转换为结构化格式的灵活性。所有方法的时间复杂度都是 O(n)。