在Python中复制一个列表的不同方法

542 阅读20分钟

在用Python编程时,我们有时需要将同一个数据存储在多个地方。这可能是由于我们需要保留原始数据的事实。在这篇文章中,我们将讨论在 python 中复制一个列表的不同方法。

内容表

  1. 在Python中复制一个列表
    1. Python 中的 id() 函数
    2. 使用 Python 中的 list() 构造函数复制一个列表
    3. 使用 Python 中的 append() 方法复制一个列表
    4. 在Python中使用extend()方法复制一个列表
    5. 在Python中使用切分法复制一个列表
    6. 在Python中使用List Comprehension复制一个列表
    7. 使用Python中的copy()方法复制列表
  2. 在 Python 中复制列表
    1. 在Python中使用append()方法复制列表
    2. 使用Python中的extend()方法复制列表
    3. 使用Python中的copy()方法复制列表
    4. 使用Python中的copy模块复制列表
  3. 总结

在Python中复制一个列表

当我们需要在Python中复制一个整数时,我们只需将一个变量分配给另一个变量,如下所示。

num1 = 10
print("The first number is:", num1)
num2 = num1
print("The copied number is:", num2)

输出。

The first number is: 10
The copied number is: 10

在这里,我们创建了一个变量num1 ,数值为10。然后,我们将num1 赋值给另一个变量num2 。在赋值之后,即使我们改变了原来的变量,复制的变量中的值也不会受到影响。 你可以在下面的例子中观察到这一点。

num1 = 10
print("The first number is:", num1)
num2 = num1
print("The copied number is:", num2)
num1 = 15
print("The first number after modification:", num1)
print("The copied number is:", num2)

输出。

The first number is: 10
The copied number is: 10
The first number after modification: 15
The copied number is: 10

在上面的例子中,你可以看到,在修改了num1 之后,num2 中的值没有被修改。

现在,让我们使用赋值操作复制一个列表。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
list2 = list1
print("The copied list is:", list2)

输出结果。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The copied list is: [1, 2, 3, 4, 5, 6, 7]

在这种情况下,当我们改变原始列表时,复制的列表也会被修改。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
list2 = list1
print("The copied list is:", list2)
list1.append(23)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list2)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 23]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7, 23]

为什么会出现这种情况?

赋值复制对整数有效,因为整数是不可改变的对象。当一个整数被分配给另一个整数时,两者都指向同一个对象。一旦我们修改任何一个整数变量,就会创建一个新的python对象,而原来的python对象则不受影响。你可以在下面的例子中观察到这一点。

num1 = 10
print("The id of first number is:", id(num1))
num2 = num1
print("The id of copied number is:", id(num2))
num1 = 15
print("The id of first number after modification:", id(num1))
print("The id of copied number after modification is:", id(num2))

输出。

The id of first number is: 9789248
The id of copied number is: 9789248
The id of first number after modification: 9789408
The id of copied number after modification is: 9789248

在这里,你可以看到使用赋值运算符将num1 复制到num2 之后,num1num2 的id都是一样的。然而,当我们修改num1 时,num1 的id发生了变化。

列表是可变的对象。当我们修改一个列表时,原来的列表对象被修改。因此,在修改列表变量的过程中,没有创建任何 python 对象,变化同时反映在复制的和原始的列表变量中。你可以在下面的例子中观察到这一点。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The id of original list is:", id(list1))
list2 = list1
print("The id of  copied list is:", id(list2))
list1.append(23)
print("The id of  original list after modification is:", id(list1))
print("The id of copied list after modification is:", id(list2))

输出。

The id of original list is: 139879630222784
The id of  copied list is: 139879630222784
The id of  original list after modification is: 139879630222784
The id of copied list after modification is: 139879630222784

在这里,你可以看到,即使在list1 中进行了修改,两个列表的 id 仍然是一样的。因此,可以确认原始列表和复制的列表即使在修改后也是指同一个对象。

由于赋值操作在 python 中对复制列表不起作用,我们将讨论在 python 中复制列表的不同方法。在这样做之前,让我们先讨论一下 python 中的id() 函数。

Python 中的 id() 函数

每个 python 对象都有一个唯一的标识符。我们可以使用id() 函数来获得与任何 python 对象相关的标识符,如上面的例子所示。

如果两个变量作为输入传给id() 函数时,给出了相同的输出,那么这两个变量指的是同一个对象。

如果id() 函数对不同的变量的输出是不同的,那么它们指的是不同的对象。

在接下来的章节中,我们将使用id() 函数来检查一个列表是否被复制了。如果一个列表被成功复制,两个列表的标识符将是不同的。在这种情况下,当我们修改一个列表时,不会影响另一个列表。

如果引用列表的两个变量的标识符都是一样的,那么这些变量引用的就是同一个列表。在这种情况下,与一个变量相关的列表中的任何变化都会反映在与另一个变量相关的列表中。

有了这个背景,现在让我们讨论一下在 python 中复制一个列表的不同方法。

使用 Python 中的 list() 构造函数复制一个列表

list() 构造函数用于从任何可迭代对象中创建一个新的 list,如 list、tuple 或 set。它接受一个可迭代对象作为其输入参数,并返回一个包含输入可迭代对象元素的新列表。

在 python 中使用list() 构造函数来复制一个列表,我们将原始列表作为输入参数传递给 list() 构造函数。

执行后, list() 构造函数将返回一个新的列表,其中包含原始列表的元素。新列表和原始列表将有不同的标识符,可以通过id() 方法获得。因此,对其中一个列表的任何改变都不会影响另一个列表。你可以在下面的例子中观察到这一点。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list(list1)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140137673798976
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 140137673851328
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]

在上面的例子中,你可以看到原始列表和复制的列表的id是不同的。因此,对原始列表的修改不会影响到复制的列表。

使用 Python 中的 append() 方法复制一个列表

append() 方法是用来向一个列表添加新的元素。当在一个列表上调用时,它把一个元素作为它的输入参数,并把它添加到列表的最后一个位置。

在 python 中使用 append() 方法复制一个列表,我们将首先创建一个名为list_copy 的空列表。之后,我们将使用for循环遍历原始列表。在迭代过程中,我们将使用append() 方法将原始列表的每个元素添加到list_copy 中。

执行for循环后,我们将在list_copy 变量中得到复制的列表。你可以在下面的例子中观察到这一点。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for element in list1:
    list_copy.append(element)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140171559405056
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 140171560708032
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]

你可以观察到新的列表和原始列表有不同的标识符,我们使用id() 方法得到了这些标识符。因此,对其中一个列表的任何改变都不会影响到另一个列表。

在 Python 中使用 extend() 方法复制一个列表

extend() 方法是用来一次向一个列表添加多个元素的。当在一个列表上调用时, extend() 方法将一个可迭代对象作为其输入参数。执行后,它将输入的可迭代对象的所有元素追加到列表中。

为了使用extend() 方法复制一个列表,我们将首先创建一个名为list_copy 的空列表。之后,我们将在list_copy 上调用extend() 方法,将原始列表作为其输入参数。执行后,我们将在list_copy 变量中得到复制的列表。

新的列表和原来的列表将有不同的标识符,可以通过id() 方法获得。因此,对其中一个列表的任何改变都不会影响另一个列表。你可以在下面的例子中观察到这一点。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
list_copy.extend(list1)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139960369243648
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 139960370546624
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]

在Python中使用分片法复制一个列表

python 中的切片是用来创建一个列表的一部分的副本的。切片的一般语法如下。

new_list=original_list[start_index:end_index]

这里。

  • new_list 是执行语句后创建的列表。
  • original_list 是给定的输入列表。
  • start_index 是必须包含在 中的最左边的元素的索引。如果我们把 留空,则默认值为 0,列表从第一个元素开始被复制。new_list start_index
  • end_index 是必须包含在 中的最右边元素的索引。如果我们把 留空,默认值是列表的长度。因此,列表被复制到最后一个元素。new_list end_index

你可以使用切片法复制一个列表,如下面的例子所示。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1[:]
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139834922264064
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 139834923567040
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]

你可以观察到新的列表和原始列表有不同的标识符,这些标识符是我们用id() 方法得到的。因此,对其中一个列表的任何改变都不会影响另一个列表。

在 Python 中使用列表理解法复制一个列表

列表理解是通过对元素施加一些条件,用现有列表的元素创建一个新的列表。列表理解的一般语法如下。

new_list=[element for element in existing_list if condition]

这里

  • new_list 是执行语句后创建的列表。
  • existing_list 是输入的列表。
  • element 代表现有列表的元素。这个变量用于遍历existing_list
  • condition 是对元素施加的条件。

在这里,我们需要使用列表理解法来复制一个给定的列表。因此,我们将不使用任何条件。下面是在 python 中使用 list comprehension 复制一个列表的代码。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = [element for element in list1]
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139720431945088
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 139720433248128

你可以观察到新的列表和原始列表有不同的标识符,这些标识符是我们使用id() 方法得到的。因此,对其中一个列表的任何改变都不会影响另一个列表。

使用 Python 中的 copy() 方法复制一个列表

Python 还为我们提供了copy() 方法来复制一个 Python 列表。copy() 方法,当在一个列表上调用时,返回一个原始列表的副本。新的列表和原始列表将有不同的标识符,可以通过id() 方法获得。因此,对其中一个列表的任何改变都不会影响另一个列表。你可以在下面的例子中观察到这一点。

list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140450078000576
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of  copied list is: 140450079303616
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]

在 Python 中复制列表的列表

在 python 中复制列表的列表与复制一个列表并不相似。例如,让我们使用copy() 方法在 python 中复制一个列表的列表。

list1 = [[1, 2, 3], [4, 5, 6,], [7,8,9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139772961010560
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 139772961063040

在这里,你可以看到原始列表和复制的列表的 id 是不同的。这意味着这两个列表是不同的 python 对象。尽管如此,当我们对原始列表进行任何修改时,它都会反映在修改后的列表中。你可以在下面的例子中观察到这一点。

list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139948423344896
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 139948423397504
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]

这种情况是不应该的。它的发生是由于内存中的列表的存储模式。一个列表包含对内部列表的引用。

当我们使用copy() 方法复制一个列表时,这个列表和内部列表的引用一起被复制了。因此,内部列表没有被复制,只有内部列表的引用被复制。由于这个原因,当我们对原始列表中的任何一个内部列表进行修改时,也会反映在复制的列表中。

为了避免这种情况,我们可以迭代地复制列表的元素。

使用 Python 中的 append() 方法复制列表的列表

要使用 python 中的append() 方法复制一个列表,我们将使用以下步骤。

  • 首先,我们将创建一个名为list_copy 的空列表。
  • 之后,我们将使用 for 循环遍历列表的列表。对于列表中的每个内部列表,我们将执行以下任务。
    • 首先,我们将创建一个名为temp 的空列表。之后,我们将使用另一个for循环来迭代内部列表的元素。
    • 在迭代过程中,我们将使用append() 方法将当前内列表的元素追加到tempappend() 方法,当在temp 上调用时,将从内部列表中获取一个元素作为输入参数,并将其追加到temp 上。
  • 在遍历了当前内列表的每个元素后,我们将把temp 追加到list_copy 。之后,我们将移动到下一个内列表并重复前面的步骤。

一旦我们遍历了输入列表的所有内列表,我们将在copy_list 变量中得到复制的列表。你可以在下面的例子中观察到这一点。

list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
    temp = []
    for element in inner_list:
        temp.append(element)
    list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139893771608960
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 139893771661952
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

迭代复制列表元素后,你可以看到原始列表中的修改并不影响复制的列表。

使用 Python 中的 extend() 方法复制列表的列表

不使用append() 方法,我们也可以使用extend() 方法在 python 中复制一个列表。为此,我们将使用以下步骤。

  • 首先,我们将创建一个名为list_copy 的空列表。
  • 之后,我们将使用for循环遍历这个列表。对于列表中的每个内部列表,我们将执行以下任务。
  • 首先,我们将创建一个名为temp 的空列表。temp 之后,我们将调用extend() 方法,将内列表作为其输入参数。
  • 然后,我们将把temp追加到list_copy 。之后,我们将移动到下一个内列表。

一旦我们遍历了输入列表的所有内列表,我们将在copy_list 变量中得到复制的列表。你可以在下面的例子中观察到这一点。

list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
    temp = []
    temp.extend(inner_list)
    list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 140175921128448
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 140175921181312
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

使用 Python 中的 copy() 方法复制列表

我们也可以使用copy() 方法来复制 Python 中的列表。为此,我们将使用以下步骤。

  • 首先,我们将创建一个名为list_copy 的空列表。
  • 之后,我们将使用for循环遍历这个列表。对于列表中的每个内部列表,我们将执行以下任务。
  • 我们将在内列表上调用copy() 方法。我们将把 copy() 方法的输出分配给一个变量temp
  • 然后,我们将把temp追加到list_copy 。之后,我们将移动到下一个内列表。

一旦我们遍历了输入列表的所有内列表,我们将在copy_list 变量中得到复制的列表。你可以在下面的例子中观察到这一点。

list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
    temp = inner_list.copy()
    list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 140468123341760
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 140468123394560
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

使用 Python 中的 copy 模块复制列表

copy 模块为我们提供了deepcopy() 方法,我们可以用它来复制嵌套对象。deepcopy() 方法将一个 python 对象作为输入参数,并递归地复制输入对象的所有元素。

我们可以使用deepcopy() 方法在 python 中复制一个列表,如下面的例子所示。

import copy
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = copy.deepcopy(list1)
print("The copied list is:", list_copy)
print("The id of  copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)

输出。

The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139677987171264
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of  copied list is: 139677987171776
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

你可以观察到,新的列表和原来的列表有不同的标识符,这些标识符是我们使用 id() 方法得到的。因此,对一个列表的任何改变都不会影响另一个列表。

总结

在这篇文章中,我们讨论了在 python 中复制一个列表的不同方法。我们还研究了在 python 中复制一个列表的不同方法。

要了解更多关于 python 编程的信息,你可以阅读这篇文章:如何在 python 中检查排序的列表。你可能也会喜欢这篇文章:如何在 python 中保存一个字典到文件

我希望你喜欢阅读这篇文章。请继续关注更多内容丰富的文章。

学习愉快!