如何在Python中加入列表?

如何在Python中加入列表?

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

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

  • 使用嵌套的for循环

  • 使用列表推导式

  • 使用sum()函数

  • 使用NumPy模块

假设我们已经拿到了一个包含一些元素的列表,我们将通过上述不同的方法将这些列表连接起来并返回结果。

阅读更多:Python 教程

第一种方法:使用嵌套的for循环

算法(步骤)

  • 创建一个变量以存储输入的列表(nested list)。

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

  • 使用for循环遍历输入的列表(nested list)的长度, len() 函数(返回一个对象中的项数)。

  • 采用另一个for循环来遍历嵌套列表中的每个元素。

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

  • 连接后输出结果列表。

示例

以下程序将返回通过嵌套for循环加入列表(view the output below)->

# 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:列表推导式

算法(步骤)

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

  • 创建一个变量以存储输入的列表(nested list)。

  • 使用 列表推导式 将所有嵌套列表中的元素连接起来创建一个新列表。

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

示例

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

# 输入嵌套列表
input_list = [["tutorialspoint", "python"], [2, 6, 7], [9, 5, 12, 7]]
print(input_list)

#使用列表推导从嵌套列表中获取每个元素并将它们存储在新列表中
resultList = [element for nestedlist in input_list for element in nestedlist]

#连接列表的列表之后打印结果列表
print("连接列表的列表后的结果列表 = ", resultList)

输出

执行上述程序会生成以下输出 −

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5, 12, 7]]
连接列表的列表后的结果列表 = ['tutorialspoint', 'python', 2, 6, 7, 9, 5, 12, 7]

方法3:使用sum()函数

算法(步骤)

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

  • 使用 sum() 函数通过将空列表作为第二个参数传递给它来将嵌套列表连接为单个列表。

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

语法

sum(iterable, start)

参数

可选的iterable − 任何序列如列表、元组等

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

  • 在连接列表的列表输入之后,打印结果列表。

示例

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

#输入的嵌套列表
input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[95]]
print(input_listoflists)

#将嵌套列表连接起来并将空列表作为第二个参数传递给sum()
resultList = sum(input_listoflists, [])

#连接列表的列表之后打印结果列表
print("连接列表的列表后的结果列表:\n", resultList)

输出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
连接列表的列表后的结果列表:
['tutorialspoint', 'python', 2, 6, 7, 9, 5]

方法4:使用NumPy模块

Numpy库包含将子字符串连接和将它们展平为单个一维列表的函数。

算法(步骤)

以下是执行期望任务所需遵循的算法/步骤−

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

  • 使用concatenate()函数连接列表,使用flat属性和list()函数(分别转换为列表)将它们展平成一个1-Dimensional列表

  • 输出连接输入列表的结果列表。

示例

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

# 导入numpy模块
import numpy

# 输入的嵌套列表
input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]]
print(input_listoflists)

# 使用concatenate()函数连接列表和使用flat属性和list()函数将它们展平成1-Dimensional列表
resultList = list(numpy.concatenate(input_listoflists).flat)

# 输出连接列表的结果列表
print("连接列表的结果列表:\n", resultList)

输出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
连接列表的结果列表:
['tutorialspoint', 'python', '2', '6', '7', '9', '5']

结论

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程