如何在 Python 中将列表附加到第二个列表(连接列表)?
在 Python 中,列表是一个有序序列,可以保存多个对象类型,例如整数、字符或浮点数。在其他编程语言中,列表相当于数组。
在本文中,我们将在 Python 中将列表附加到另一个列表(连接列表)。以下是完成此任务的不同方法 -
使用连接 (+) 运算符
使用列表对象的 append 方法
使用 extend() 方法
使用 itertools.chain() 方法
获取没有重复的列表
假设我们已经获取了一个包含一些元素的列表。我们将使用上面指定的方法返回给定的 2 个输入列表的连接列表
方法 1:使用连接 (+) 运算符
连接运算符 (+) 是 Python 中连接列表的最常用方法。如下面的示例所示,"+"运算符可以轻松地将整个列表连接到另一个列表后面,并返回新列表作为结果输出
使用连接运算符 (+) 将第二个列表添加到第一个列表并将其存储在第一个列表中。
示例
以下程序使用 (+) 运算符返回给定的两个输入列表的连接列表 -
# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)
输出
执行时,上述程序将生成以下输出 -
First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']
方法 2:使用列表对象的 append() 方法
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 −/p>
创建一个变量来存储第一个输入列表,并用一些随机值初始化它。同样,使用另一组随机值创建另一个列表(第二个输入列表)。
通过将第二个输入列表作为参数传递给 append() 函数(将元素添加到列表末尾),将第二个输入列表附加到第一个输入列表。
将第一个输入列表与第二个输入列表连接后打印。
示例
以下程序使用 append() 方法返回给定两个输入列表的连接列表 −
# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the append() function givenList1.append(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)
输出
执行时,上述程序将生成以下输出
First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, ['python', 'code']]
方法 3:使用 extend() 方法
要在 Python 中连接两个列表,我们可以使用 extend() 函数。extend() 函数迭代给定的参数并将项目添加到列表中,从而线性扩展列表。
语法
list.extend(iterable)
示例
以下程序使用 extend() 方法返回给定两个输入列表的连接列表 -
# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the extend() function givenList1.extend(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)
输出
First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']
方法 4:使用 itertools.chain() 方法
itertools.chain() 函数在将其参数链接在一起后返回可迭代对象,因此如果只需要初始迭代,则不需要存储连接列表。当只需要一次连接列表时,这很方便
示例
以下程序使用 itertools.chain() 函数返回给定两个输入列表的连接列表 -
import itertools # first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using itertools.chain() function givenList1 = list(itertools.chain(givenList1, givenList2)) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)
输出
First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']
方法 5:获取没有重复的列表
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 −/p>
创建一个变量来存储第一个输入列表,并用一些随机值初始化它。类似地,使用另一组随机值创建另一个列表(第二个输入列表)。
使用连接运算符 (+) 将第二个列表添加到第一个列表,并将其存储在第一个列表中。
使用 set() 函数将第一个列表转换为集合(它会删除重复项),然后使用 list() 函数将此集合转换回列表。
打印结果列表,即没有重复项的列表。
示例
以下程序返回给定两个输入列表的连接列表,没有重复项 -
# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code", 20, "TutorialsPoint"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Removing duplicates from the concatenated list uniqueList=list(set(givenList1)) # Printing the concatenated list after removing duplicates print ("Concatenated list after removing duplicates: ", uniqueList)
输出
Concatenated list after removing duplicates: ['TutorialsPoint', 10, 'Hello', 'python', 20, 'code']
结论
我们学习了如何使用四种不同的方法将一个列表附加到另一个列表(列表连接),包括连接运算符 (+)、append()、extend() 和 chain() 函数。我们还学习了如何从连接列表中删除重复项。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

