Python - 从列表创建嵌套记录列表


我们手头的问题是,必须使用 Python 创建一个算法,以便从给定的多个列表中获取嵌套记录列表。有时,出于实际应用中的某些原因,我们需要合并给定的列表。因此,解决此问题将有助于解决这些问题。

理解问题的逻辑

在此问题中,我们将获得两个或多个列表,并且必须通过应用逻辑来组合并形成嵌套记录列表。因此,我们将使用不同的方法来完成此任务。

首先,我们将使用 zip 方法压缩所有作为输入给出的列表,zip 方法将生成单个嵌套列表。其次,我们将使用带有 zip 方法的循环并创建一个新列表,该列表将包含给定输入列表的所有项。第三,我们将使用用户定义函数解决此问题。

第一种方法 - 算法

在此方法中,我们将使用 Python 的 zip 方法,该方法将根据列表项的索引组合列表。

  • 步骤 1 - 初始化列表。在我们的代码中,我们将使用 Name、DOB 和 Birth_Place 列表。

  • 步骤 2 - 现在使用名为 combined_lists 的新列表,此列表将存储从给定列表生成的嵌套记录列表。并使用 zip 方法,我们将通过在 zip 方法中传递所有列表来创建此列表。

  • 步骤 3 - 打印 combined_lists 以显示输出。

示例

Name= ["John","Riya", "Amit"]

DOB = ["11-09-1998", "17-02-1993", "02-05-2003"]

Birth_Place = ["Delhi", "Mumbai", "Pune"]

combine_lists = zip(Name, DOB, Birth_Place)

print("The combined list for all the lists")

print(list(combine_lists))

输出

The combined list for all the lists
[('John', '11-09-1998', 'Delhi'), ('Riya', '17-02-1993', 'Mumbai'), ('Amit', '02-05-2003', 'Pune')]

第二种方法 - 算法

这是使用 for 循环与 zip 方法完成此任务的另一种方法。

  • 步骤 1 - 定义 list1 和 list 2,它们将用于组合以创建嵌套列表。

  • 步骤 2 - 使用 the_key 变量在新的列表中添加键,其值将为 id。

  • 步骤 3 - 现在,我们将创建一个名为 nested_list 的新列表,并通过为每个项目创建键来初始化它,然后使用 zip 方法在外部列表中再次创建嵌套列表。

  • 步骤 4 - 获取 nested_list 后,我们将打印输入和输出列表的值。

示例

# Initialize the lists
list1 = ['Tea', 'Coffee']
list2 = [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]

the_key = 'id'

nested_list = {key : [{the_key : index} for index in val]
   for key, val in zip(list1, list2)}

# Print the input lists
print("The first input list : " + str(list1))
print("The second input list : " + str(list2))

# Result print on the console
print("The constructed dictionary is : " + str(nested_list))

输出

The first input list : ['Tea', 'Coffee']
The second input list : [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]
The constructed dictionary is : {'Tea': [{'id': 'Black'}, {'id': 'Green'}, {'id': 'White'}], 'Coffee': [{'id': 'Espresso'}, {'id': 'Latte'}, {'id': 'Cappuccino'}]}

第三种方法 - 算法

在此方法中,我们将定义一个函数,以使用多个输入列表生成嵌套列表。

  • 步骤 1 - 首先定义名为 generate_nested_list 的函数,并将所有输入列表作为参数传递给它。在这里,我们将使用星号或星号运算符 (*) 通过单个带有星号运算符的变量传递多个变量。

  • 步骤 2 - 现在定义一个空白列表作为 combined_list,它将存储结果嵌套列表。

  • 步骤 3 - 现在,我们将使用 for 循环组合列表,并将所有项目追加到 combined_list 中。

  • 步骤 4 - 通过调用函数并向函数中传递所有列表来打印 combined_list。

示例

# Define the function to generate nested list
def generate_nested_list(*lists):
   combined_list = []
   for lst in lists:
      combined_list.append(lst)
   return combined_list

#Initialize the lists to generate nested list
list1 = ['I', 'am', 'using']
list2 = ['Tutorials', 'point', 'to']
list3 = ['learn', 'Python', 'Programming']
nested_list = generate_nested_list(list1, list2, list3)
print("The created nested list is as follows\n", nested_list)

输出

The created nested list is as follows
 [['I', 'am', 'using'], ['Tutorials', 'point', 'to'], ['learn', 'Python', 'Programming']]

复杂度

从给定列表创建新的嵌套记录列表的时间复杂度为 O(n),其中 n 是所有列表中存在的项目总数。因为我们对所有方法都执行了简单的操作,这些操作需要线性时间才能完成任务。

结论

由于我们已经使用 Python 中的各种方法成功地解决了给定的问题。因此,在这里我们学习了如何使用简单的方法从给定的多个列表组合或创建嵌套列表。当我们需要从给定的多个数据集合并和创建一个新的数据集时,此任务可能会有所帮助。

更新于: 2023年10月17日

93 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.