Python 简明教程

Python - Copy Lists

Copying a List in Python

在 Python 中复制列表是指创建一个包含与此原始列表相同元素的新列表。有不同的方法可以复制列表,包括使用切片表示法、list() 函数和使用 copy() 方法。

Copying a list in Python refers to creating a new list that contains the same elements as the original list. There are different methods for copying a list, including, using slice notation, the list() function, and using the copy() method.

每种方法在创建浅层副本还是深层副本方面行为有所不同。我们将在本教程中深入讨论所有这些内容。

Each method behaves differently in terms of whether it creates a shallow copy or a deep copy. Let us discuss about all of these deeply in this tutorial.

Shallow Copy on a Python List

Python 中的浅层副本创建一个新对象,不过它会复制对原始元素的引用,而不是递归复制元素。这意味着这个新对象与原始对象是独立的实体,但如果元素本身是可变的,那么在新对象中对这些元素所做的更改也将影响原始对象。

A shallow copy in Python creates a new object, but instead of copying the elements recursively, it copies only the references to the original elements. This means that the new object is a separate entity from the original one, but if the elements themselves are mutable, changes made to those elements in the new object will affect the original object as well.

Example of Shallow Copy

让我们用以下示例来说明这一点 −

Let us illustrate this with the following example −

import copy
# Original list
original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Creating a shallow copy
shallow_copied_list = copy.copy(original_list)
# Modifying an element in the shallow copied list
shallow_copied_list[0][0] = 100
# Printing both lists
print("Original List:", original_list)
print("Shallow Copied List:", shallow_copied_list)

正如你所看到的,尽管我们只修改了浅层复制的列表中第一子列表的第一个元素,但原始列表中也会反映出同样的改动。

As you can see, even though we only modified the first element of the first sublist in the shallow copied list, the same change is reflected in the original list as well.

这是因为浅层副本只会创建对原始对象的新的引用,并不会创建对象本省的副本 −

This is because a shallow copy only creates new references to the original objects, rather than creating copies of the objects themselves −

Original List: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]
Shallow Copied List: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]

Deep Copy on a Python List

Python 中的深层副本会创建一个完全新的对象并递归复制原始对象引用的所有对象。这意味着原始对象中嵌套的对象也会被复制,最终生成一份完全独立的副本,该副本中的改动不会影响原始对象,反之亦然。

A deep copy in Python creates a completely new object and recursively copies all the objects referenced by the original object. This means that even nested objects within the original object are duplicated, resulting in a fully independent copy where changes made to the copied object do not affect the original object, and vice versa.

Example of Deep Copy

让我们用以下示例来说明这一点 −

Let us illustrate this with the following example −

import copy
# Original list
original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Creating a deep copy
deep_copied_list = copy.deepcopy(original_list)
# Modifying an element in the deep copied list
deep_copied_list[0][0] = 100
# Printing both lists
print("Original List:", original_list)
print("Deep Copied List:", deep_copied_list)

正如你所看到的,当我们修改深层复制的列表中第一子列表的第一个元素时,原始列表并不会受到影响。

As you can see, when we modify the first element of the first sublist in the deep copied list, it does not affect the original list.

这是因为深层副本会创建一个新对象并递归复制所有嵌套对象,以确保复制的对象完全独立于原始对象 −

This is because a deep copy creates a new object and recursively copies all the nested objects, ensuring that the copied object is fully independent from the original one −

Original List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Deep Copied List: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]

Copying List Using Slice Notation

Python 中的切片表示法允许你通过指定起始索引、结束索引和可选步长来从序列(如列表、元组或字符串)中创建元素的子序列。切片表示法的语法如下 −

Slice notation in Python allows you to create a subsequence of elements from a sequence (like a list, tuple, or string) by specifying a start index, an end index, and an optional step size. The syntax for slice notation is as follows −

[start:end:step]

其中, start 是切片开始的索引, end 是切片结束的索引(不包括), step 是元素间的步长。

Where, start is the index where the slice starts, end is the index where the slice ends (exclusive), and step is the step size between elements.

我们可以通过指定原始列表的所有索引范围来使用切片表示法复制列表。这样实际上会创建一个包含与原始列表相同元素的新列表。

We can copy a list using slice notation by specifying the entire range of indices of the original list. This effectively creates a new list with the same elements as the original list.

Example

在此示例中,我们正在创建 "original_list" 的切片,实际上是将它的所有元素复制到新的列表 "copied_list" 中 −

In this example, we are creating a slice of the "original_list", effectively copying all its elements into a new list "copied_list" −

# Original list
original_list = [1, 2, 3, 4, 5]
# Copying the list using slice notation
copied_list = original_list[1:4]
# Modifying the copied list
copied_list[0] = 100
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)

我们得到的如下结果 −

We get the result as shown below −

Original List: [1, 2, 3, 4, 5]
Copied List: [100, 3, 4]

Copying List Using the list() Function

Python 中的 list() 函数是一个内置函数,用于创建一个新的列表对象。它可以接受一个可迭代对象(如另一个列表、元组、集合等等)作为参数并创建一个包含该可迭代对象元素的新列表。如果未提供参数,则会创建一个空列表。

The list() function in Python is a built-in function used to create a new list object. It can accept an iterable (like another list, tuple, set, etc.) as an argument and create a new list containing the elements of that iterable. If no argument is provided, an empty list is created.

我们可以通过将原始列表作为参数传递来使用 list() 函数复制一个列表。这将创建一个包含与原始列表相同元素的新列表对象。

We can copy a list using the list() function by passing the original list as an argument. This will create a new list object containing the same elements as the original list.

Example

在下面的示例中,我们正在使用 list() 函数创建一个新列表对象 "copied_list",它包含与 "original_list" 相同的元素 −

In the example below, we are creating a new list object "copied_list" containing the same elements as "original_list" using the list() function −

# Original list
original_list = [1, 2, 3, 4, 5]
# Copying the list using the list() constructor
copied_list = list(original_list)
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)

以下是上面代码的输出: -

Following is the output of the above code −

Original List: [1, 2, 3, 4, 5]
Copied List: [1, 2, 3, 4, 5]

Copying List Using the copy() Function

在 Python 中,copy() 函数用于创建列表或其他可变对象的浅层副本。此函数是 Python 标准库中 copy 模块的一部分。

In Python, the copy() function is used to create a shallow copy of a list or other mutable objects. This function is part of the copy module in Python’s standard library.

我们可以通过调用原始列表上的copy()函数来复制一个列表。这会创建一个新的列表对象,它包含与原始列表相同的元素。

We can copy a list using the copy() function by invoking it on the original list. This creates a new list object that contains the same elements as the original list.

Example

在以下示例中,我们正在使用copy()函数创建一个包含与original_list相同元素的新列表对象copied_list −

In the following example, we are using the copy() function to creates a new list object "copied_list" containing the same elements as "original_list" −

import copy
original_list = [1, 2, 3, 4, 5]
# Copying the list using the copy() function
copied_list = copy.copy(original_list)
print("Copied List:", copied_list)

以上代码的输出如下所示 −

Output of the above code is as shown below −

Copied List: [1, 2, 3, 4, 5]