Python 简明教程
Python - Join Lists
Join Lists in Python
Joining lists in Python refers to combining the elements of multiple lists into a single list. This can be achieved using various methods, such as concatenation, list comprehension, or using built-in functions like extend() or + operator.
链接列不会修改原始列,而是创建一个包含组合元素的新列。
Joining lists does not modify the original lists but creates a new list containing the combined elements.
Join Lists Using Concatenation Operator
Python 中的连接运算符用 + 表示,用于将两个序列(如字符串、列表或元组)连接成一个序列。当应用于列表时,连接运算符将两个(或更多)列表的元素组合起来,创建一个新列表,其中包含来自两个列表的所有元素。
The concatenation operator in Python, denoted by +, is used to join two sequences, such as strings, lists, or tuples, into a single sequence. When applied to lists, the concatenation operator joins the elements of the two (or more) lists to create a new list containing all the elements from both lists.
我们可以简单地使用连接运算符 + 来连接列表,以连接列表。
We can join a list using the concatenation operator by simply using the + symbol to concatenate the lists.
Example
在以下示例中,我们连接两个列表“L1”和“L2”的元素,创建一个包含来自两个列表的所有元素的新列表“joined_list”−
In the following example, we are concatenating the elements of two lists "L1" and "L2", creating a new list "joined_list" containing all the elements from both lists −
# Two lists to be joined
L1 = [10,20,30,40]
L2 = ['one', 'two', 'three', 'four']
# Joining the lists
joined_list = L1 + L2
# Printing the joined list
print("Joined List:", joined_list)
以下是上面代码的输出: -
Following is the output of the above code −
Joined List: [10, 20, 30, 40, 'one', 'two', 'three', 'four']
Join Lists Using List Comprehension
List comprehension 是在 Python 中创建列表的一种简洁方法。它用于通过将表达式应用于现有可迭代对象(如列表、元组或范围)中的每个项来生成新列表。列表推导的语法为 −
List comprehension is a concise way to create lists in Python. It is used to generate new lists by applying an expression to each item in an existing iterable, such as a list, tuple, or range. The syntax for list comprehension is −
new_list = [expression for item in iterable]
这将创建一个新列表,其中针对 iterable 中的每个 item 求 expression 。
This creates a new list where expression is evaluated for each item in the iterable.
我们可以通过迭代多个列表并将它们的元素附加到新列表来使用列表推导连接列表。
We can join a list using list comprehension by iterating over multiple lists and appending their elements to a new list.
Example
在此示例中,我们使用列表推导将两个列表 L1 和 L2 连接成一个列表。最终列表 joined_list 包含 L1 和 L2 中的所有元素 −
In this example, we are joining two lists, L1 and L2, into a single list using list comprehension. The resulting list, joined_list, contains all elements from both L1 and L2 −
# Two lists to be joined
L1 = [36, 24, 3]
L2 = [84, 5, 81]
# Joining the lists using list comprehension
joined_list = [item for sublist in [L1, L2] for item in sublist]
# Printing the joined list
print("Joined List:", joined_list)
上述代码的输出如下:
Output of the above code is as follows −
Joined List: [36, 24, 3, 84, 5, 81]
Join Lists Using append() Function
Python 中的 append() 函数用于向列表末尾添加单个元素。此函数通过将元素添加到列表末尾来修改原始列表。
The append() function in Python is used to add a single element to the end of a list. This function modifies the original list by adding the element to the end of the list.
我们可以通过迭代一个列表中的元素并将每个元素附加到另一个列表来使用 append() 函数连接列表。
We can join a list using the append() function by iterating over the elements of one list and appending each element to another list.
Example
在下面的示例中,我们使用 append() 函数将“list2”中的元素附加到“list1”。我们通过迭代“list2”并向“list1”添加每个元素来实现此目的 −
In the example below, we are appending elements from "list2" to "list1" using the append() function. We achieve this by iterating over "list2" and adding each element to "list1" −
# List to which elements will be appended
list1 = ['Fruit', 'Number', 'Animal']
# List from which elements will be appended
list2 = ['Apple', 5, 'Dog']
# Joining the lists using the append() function
for element in list2:
list1.append(element)
# Printing the joined list
print("Joined List:", list1)
我们得到了如下输出 −
We get the output as shown below −
Joined List: ['Fruit', 'Number', 'Animal', 'Apple', 5, 'Dog']
Join Lists Using extend() Function
Python extend() 函数用于将可迭代对象(如另一个列表)中的元素附加到列表末尾。此函数直接修改原始列表,将可迭代对象中的元素添加到列表末尾。
The Python extend() function is used to append elements from an iterable (such as another list) to the end of the list. This function modifies the original list in place, adding the elements of the iterable to the end of the list.
我们可以通过在一个列表上调用它并传递另一个列表(或任何可迭代对象)作为参数来使用 extend() 函数连接列表。这会将第二个列表中所有元素附加到第一个列表的末尾。
We can join a list using the extend() function by calling it on one list and passing another list (or any iterable) as an argument. This will append all the elements from the second list to the end of the first list.
Example
在以下示例中,我们使用 extend() 函数通过附加“list2”的元素来扩展“list1”−
In the following example, we are extending "list1" by appending the elements of "list2" using the extend() function −
# List to be extended
list1 = [10, 15, 20]
# List to be added
list2 = [25, 30, 35]
# Joining the lists using the extend() function
list1.extend(list2)
# Printing the extended list
print("Extended List:", list1)
获得的输出如下所示 −
The output obtained is as shown below −
Extended List: [10, 15, 20, 25, 30, 35]