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


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

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

  • 使用嵌套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()时会发生什么。

更新于:2022年9月19日

27K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.