如何在 Python 中连接列表列表?

pythonserver side programmingprogramming更新于 2025/5/4 18:52:17

在 Python 中,列表是一个有序序列,可以容纳多种对象类型,例如整数、字符或浮点数。

在本文中,我们将向您展示如何使用 Python 连接列表列表(嵌套列表)。现在我们看到了完成此任务的 4 种方法 -

  • 使用嵌套 for 循环

  • 使用列表推导式

  • 使用 sum() 函数

  • 使用 NumPy 模块

假设我们已经获取了一个包含一些元素的列表列表。我们将使用上面指定的不同方法将这些列表合并并返回结果。

方法 1:使用嵌套 for 循环

算法(步骤)

  • 创建一个变量来存储输入的列表列表(嵌套列表)。

  • 创建一个新的空列表来存储结果列表

  • 使用 for 循环,使用 len()函数遍历输入列表的长度(对象中的项目数由 len() 方法返回)

  • 使用另一个 for 循环遍历嵌套列表的每个元素

  • 使用 append() 函数(将元素添加到列表末尾)将此元素添加到结果中列表。

  • 连接输入列表后打印结果列表。

示例

以下程序使用嵌套 for 循环连接输入列表后返回列表 -

# input list of lists (nested list) input_nestedlist = [[1, 3],[2, 6, 7],[9, 5, 12, 7]] print(input_nestedlist) # creating a new empty list for storing result resultList = [] # Traversing in till the length of the input list of lists for m in range(len(input_nestedlist)): # using nested for loop, traversing the inner lists for n in range (len(input_nestedlist[m])): # Add each element to the result list resultList.append(input_nestedlist[m][n]) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists = ", resultList)

输出

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

[[1, 3], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining the list of lists = [1, 3, 2, 6, 7, 9, 5, 12, 7]

方法 2:列表推导式

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 创建一个变量来存储输入的列表列表(嵌套列表)。

  • 使用列表推导式通过连接嵌套列表的所有元素来创建新列表

当您想要根据现有列表的值创建新列表时,列表推导式提供了简洁的语法。
  • 连接输入列表后打印结果列表。

示例

以下程序使用列表推导连接输入列表后返回列表 -

# input list of lists (nested list) input_list = [["tutorialspoint", "python"], [2, 6, 7], [9, 5, 12, 7]] print(input_list) # Getting each element from nested Lists and storing them in a new list using list comprehension resultList = [element for nestedlist in input_list for element in nestedlist] # printing the resultant list after joining the list of lists print("Resultant list after joining list of lists = ", resultList)

输出

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

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining list of lists = ['tutorialspoint', 'python', 2, 6, 7, 9, 5, 12, 7]

方法 3:使用 sum() 函数

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 sum() 函数将嵌套列表连接到单个列表,方法是将空列表作为第二个参数传递给它。

sum() 函数返回一个数字,该数字表示可迭代对象中所有项目的总和。

语法

sum(iterable, start)

参数

iterable(可选)− 任何序列,如列表、元组等

start(可选)− 附加/添加到返回值的值

  • 连接输入列表后打印结果列表。

示例

以下程序使用 sum() 函数连接输入列表后返回列表 −

# input nested lists input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # pass second argument as empty list to concatenate nested lists resultList = sum(input_listoflists, []) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

输出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', 2, 6, 7, 9, 5]

方法 4:使用 NumPy 模块

Numpy 库包含用于连接子字符串并将其展平为单个一维列表的函数。

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 import 关键字导入 NumPy 模块

  • 使用 concatenate() 函数连接列表列表,然后分别使用 flat 属性和 list() 函数(转换为列表)将它们展平为一维列表

  • 连接输入列表后打印结果列表列表。

示例

以下程序在使用 NumPy 模块连接输入列表后返回列表 -

# importing numpy module import numpy # input list of lists (nested list) input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # joining the list of lists using the concatenate() function and # flattening them into a 1-Dimensional list using the flat attribute # and list() function respectively resultList = list(numpy.concatenate(input_listoflists).flat) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

输出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', '2', '6', '7', '9', '5']

结论

从本文中,我们学习了如何使用四种不同的方法将列表列表连接/串联为一维列表,包括 for 循环、列表推导、NumPy 函数和 sum() 函数。我们还了解了将带有空列表的嵌套列表传递给 sum() 时会发生什么。


相关文章


有用资源