如何在 Python 中连接列表的列表?
在 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:列表推导式
算法(步骤)
以下是执行所需任务的算法/步骤:
创建一个变量来存储输入的列表的列表(嵌套列表)。
使用 列表推导式通过连接嵌套列表的所有元素来创建一个新列表
When you want to create a new list based on the values of an existing list, list comprehension provides a concise syntax.
连接输入列表的列表后打印结果列表。
示例
以下程序使用列表推导式连接输入列表的列表后返回该列表:
# 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() 时会发生什么。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP