Python 简明教程
Python - Join Arrays
将两个数组连接的过程称为合并或串联。Python 提供多种合并两个数组的方式,例如 append() 和 extend() 方法。但是,在合并两个数组之前,始终确保两个数组具有相同的数据类型,否则程序将抛出错误。
The process of joining two arrays is termed as Merging or concatenating. Python provides multiple ways to merge two arrays such as append() and extend() methods. But, before merging two arrays always ensure that both arrays are of the same data type otherwise program will throw error.
在 Python 中, array 是 Python 内置 data types (如 strings 、整数或浮点数对象)的同构集合。但是,数组本身不是内置类型,而需要使用 Python 的内置数组模块。
In Python, array is a homogenous collection of Python’s built in data types such as strings, integer or float objects. However, array itself is not a built-in type, instead we need to use the Python’s built-in array module.

Join two Arrays in Python
要合并 Python 中的数组,请使用以下方法:
To join arrays in Python, use the following approaches −
-
Using append() method
-
Using + operator
-
Using extend() method
Using append() Method
我们使用 append() 方法将一个数组中的每一项追加到另一个数组中。要执行此操作,针对原始数组运行 for loop ,提取每个元素,然后将其附加到新数组中。
To join two arrays, we can append each item from one array to other using append() method. To perform this operation, run a for loop on the original array, fetch each element and append it to a new array.
Example: Join Two Arrays by Appending Elements
在这里,我们使用 append() 方法加入两个数组。
Here, we use the append() method to join two arrays.
import array as arr
# creating two arrays
a = arr.array('i', [10,5,15,4,6,20,9])
b = arr.array('i', [2,7,8,11,3,10])
# merging both arrays
for i in range(len(b)):
a.append(b[i])
print (a)
它将生成以下 output −
It will produce the following output −
array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])
Using + operator
我们还可以使用 + 运算符连接或合并两个数组。在这种方法中,我们首先将数组转换为 list 对象,然后使用 + 运算符连接列表,再转换回来以获取合并的数组。
We can also use + operator to concatenate or merge two arrays. In this approach, we first convert arrays to list objects, then concatenate the lists using the + operator and convert back to get merged array.
Example: Join Two Arrays by Converting to List Objects
在这个示例中,我们将看到如何使用 + 运算符连接两个数组。
In this example, we will see how to join two arrays using + operator.
import array as arr
a = arr.array('i', [10,5,15,4,6,20,9])
b = arr.array('i', [2,7,8,11,3,10])
x = a.tolist()
y = b.tolist()
z = x+y
a = arr.array('i', z)
print (a)
上述代码将显示以下输出 −
The above code will display the following output −
array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])
Using extend() Method
连接数组的另一种方法是从 List 类中使用 extend() method 。与上述方法类似,我们首先将数组转换为列表,然后再调用 extend() 方法来合并这两个列表。
Another approach to concatenate arrays is the use of extend() method from the List class. Similar to above approach, we first convert the array to a list and then call the extend() method to merge the two lists.
Example: Join Two Arrays using extend() Method
在以下示例中,我们将使用 extend() 方法在 Python 中连接两个数组。
In the following example, we will use the extend() method to concatenate two arrays in Python.
import array as arr
a = arr.array('i', [88, 99, 77, 66, 44, 22])
b = arr.array('i', [12, 17, 18, 11, 13, 10])
a.extend(b)
print (a)
它将生成以下 output −
It will produce the following output −
array('i', [88, 99, 77, 66, 44, 22, 12, 17, 18, 11, 13, 10])