Python 简明教程

Python - Copy Arrays

在 Python 中,复制数组是指创建包含原始数组所有元素的新数组的过程。此操作可以使用赋值运算符 (=) 和 deepcopy() 方法完成。在本章中,我们将讨论如何 copy an array object to another 。但是,在进入详情之前,让我们简要讨论数组。

In Python, copying an array refers to the process of creating a new array that contains all the elements of the original array. This operation can be done using assignment operator (=) and deepcopy() method. In this chapter, we discuss how to copy an array object to another. But, before getting into the details let’s breifly discuss arrays.

Python 的内置序列类型,即 listtuplestring 是按索引排列的项集合。但是,与 C/C++、Java 等语言中的数组不同,它们不是同构的,这意味着这些类型的集合中的元素可能属于不同类型。Python 的 array 模块可帮助你创建类似于 Java 数组的对象。

Python’s built-in sequence types i.e. list, tuple, and string are indexed collection of items. However, unlike arrays in C/C++, Java etc. they are not homogenous, in the sense the elements in these types of collection may be of different types. Python’s array module helps you to create object similar to Java like arrays.

Python arrays 可以是字符串、整数或浮点型。array 类构造函数的使用方式如下:

Python arrays can be of string, integer or float type. The array class constructor is used as follows −

import array
obj = array.array(typecode[, initializer])

其中,类型代码可以是表示数据类型的字符常量。

Where, the typecode may be a character constant representing the data type.

Copy Arrays Using Assignment Operator

我们可以使用赋值运算符 (=) 将一个数组分配给另一个数组。但是,此类赋值不会在内存中创建新数组。相反,它会创建一个对同一数组的新引用。

We can assign an array to another by using the assignment operator (=). However, such assignment doesn’t create a new array in the memory. Instead, it creates a new reference to the same array.

Example

在以下示例中,我们使用赋值运算符在 Python 中复制数组。

In the following example, we are using assignment operator to copy array in Python.

import array as arr
a = arr.array('i', [110, 220, 330, 440, 550])
b = a
print("Copied array:",b)
print (id(a), id(b))

它将生成以下 output

It will produce the following output

Copied array: array('i', [110, 220, 330, 440, 550])
134485392383792 134485392383792

检查 a 和 b 的 id()。相同的 id 值可以确认简单的赋值不会创建副本。由于“a”和“b”引用同一数组对象,因此数组“a”中的任何更改也会反映在“b”中:

Check the id() of both a and b. Same value of id confirms that simple assignment doesn’t create a copy. Since "a" and "b" refer to the same array object, any change in the array "a" will reflect in "b" too −

a[2] = 10
print (a,b)

它将生成以下 output

It will produce the following output

array('i', [110, 220, 10, 440, 550]) array('i', [110, 220, 10, 440, 550])

Copy Arrays Using Deep Copy

若要创建数组的另一个物理副本,我们将使用 Python 库中的另一个模块,名为 copy,并使用该模块中的 deepcopy() 函数。深度复制会构建一个新的复合对象,然后将原始对象中找到的对象的副本递归插入到这个复合对象中。

To create another physical copy of an array, we use another module in Python library, named copy and use deepcopy() function in the module. A deep copy constructs a new compound object and then, recursively inserts copies into it of the objects found in the original.

Example

以下示例演示如何在 Python 中复制数组:

The following example demonstrates how to copy array in Python −

import array as arr
import copy
a = arr.array('i', [110, 220, 330, 440, 550])
b = copy.deepcopy(a)
print("Copied array:",b)

执行后,将产生以下 output

On executing, it will produce the following output

Copied array: array('i', [110, 220, 330, 440, 550])

现在检查 "a" 和 "b" 的 id()。你会发现 id 是不同的。

Now check the id() of both "a" and "b". You will find the ids are different.

print (id(a), id(b))

它将生成以下 output

It will produce the following output

2771967069936 2771967068976

这证明创建了一个新对象 "b",它实际上是 "a" 的副本。如果我们在 "a" 中更改一个元素,它不会反映在 "b" 中。

This proves that a new object "b" is created which is an actual copy of "a". If we change an element in "a", it is not reflected in "b".

a[2]=10
print (a,b)

它将生成以下 output

It will produce the following output

array('i', [110, 220, 10, 440, 550]) array('i', [110, 220, 330, 440, 550])