Python 简明教程
Python - Arrays
Arrays in Python
与 C++ 或 Java 等其他编程语言不同,Python 没有对数组的内置支持。但是,Python 具有多种数据类型,如列表和元组(尤其是列表),它们通常用作数组,但存储在这些类型序列中的项不必属于同一种类型。
Unlike other programming languages like C++ or Java, Python does not have built-in support for arrays. However, Python has several data types like lists and tuples (especially lists) that are often used as arrays but, items stored in these types of sequences need not be of the same type.
此外,我们可以使用 array 模块创建和操作数组。在继续之前,让我们了解一下数组的一般情况。
In addition, we can create and manipulate arrays the using the array module. Before proceeding further, let’s understand arrays in general.
What are arrays?
array 是一个容器,它可以容纳固定数量的项,并且这些项应该属于同一种类型。存储在数组中的每一项都称为 element ,它们可以是任意类型,包括整数、浮点数、字符串等。
An array is a container which can hold a fix number of items and these items should be of the same type. Each item stored in an array is called an element and they can be of any type including integers, floats, strings, etc.
这些元素存储在连续的内存位置。数组中每个元素的位置都有一个从 0 开始的数字 index 。这些索引用于标识和访问元素。
These elements are stored at contiguous memory location. Each location of an element in an array has a numerical index starting from 0. These indices are used to identify and access the elements.
Array Representation
数组表示为多个容器的集合,其中每个容器存储一个元素。这些容器的索引从“0”到“n-1”,其中 n 是特定数组的大小。
Arrays are represented as a collection of multiple containers where each container stores one element. These containers are indexed from '0' to 'n-1', where n is the size of that particular array.
在不同的语言中,可以用各种方式声明数组。下面是一个插图:
Arrays can be declared in various ways in different languages. Below is an illustration −
根据上述插图,以下是要考虑的重要要点:
As per the above illustration, following are the important points to be considered −
-
Index starts with 0.
-
Array length is 10 which means it can store 10 elements.
-
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Creating Array in Python
要在 Python 中创建数组,请导入 array 模块并使用其 array() 函数。我们可以使用此函数创建三种基本类型的数组,即整数、浮点数和 Unicode 字符。
To create an array in Python, import the array module and use its array() function. We can create an array of three basic types namely integer, float and Unicode characters using this function.
array() 函数接受 typecode 和 initializer 作为参数值,并返回 array 类的对象。
The array() function accepts typecode and initializer as a parameter value and returns an object of array class.
Syntax
在 Python 中创建数组的语法是 -
The syntax for creating an array in Python is −
# importing
import array as array_name
# creating array
obj = array_name.array(typecode[, initializer])
其中,
Where,
-
typecode − The typecode character used to speccify the type of elements in the array.
-
initializer − It is an optional value from which array is initialized. It must be a list, a bytes-like object, or iterable elements of the appropriate type.
Example
以下示例演示如何使用 array 模块在 Python 中创建数组。
The following example shows how to create an array in Python using the array module.
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
print (type(a), a)
# creating an array with char type
a = arr.array('u', 'BAT')
print (type(a), a)
# creating an array with float type
a = arr.array('d', [1.1, 2.2, 3.3])
print (type(a), a)
它将生成以下 output −
It will produce the following output −
<class 'array.array'> array('i', [1, 2, 3])
<class 'array.array'> array('u', 'BAT')
<class 'array.array'> array('d', [1.1, 2.2, 3.3])
Python 数组类型由单个字符类型代码参数决定。类型代码和数组的目标数据类型如下所列: -
Python array type is decided by a single character Typecode argument. The type codes and the intended data type of array is listed below −
Basic Operations on Python Arrays
以下是数组支持的基本操作 -
Following are the basic operations supported by an array −
-
Traverse − Print all the array elements one by one.
-
Insertion − Adds an element at the given index.
-
Deletion − Deletes an element at the given index.
-
Search − Searches an element using the given index or by the value.
-
Update − Updates an element at the given index.
Accessing Array Element
我们可以使用元素的索引访问数组的每个元素。
We can access each element of an array using the index of the element.
Insertion Operation
在插入操作中,我们将一个或多个数据元素插入到数组中。基于需求,可以在数组的开头、结尾或任何给定索引处添加新元素。
In insertion operation, we insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
Example
在此,我们使用 Python 内置的 insert() 方法在数组中间添加了一个数据元素。
Here, we add a data element at the middle of the array using the python in-built insert() method.
from array import *
array1 = array('i', [10,20,30,40,50])
array1.insert(1,60)
for x in array1:
print(x)
当我们编译并执行上述程序时,它会产生以下结果,显示元素插入到索引位置 1。
When we compile and execute the above program, it produces the following result which shows the element is inserted at index position 1.
10
60
20
30
40
50
Deletion Operation
删除指的是从数组中移除现有元素并重新组织数组的所有元素。
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
在此,我们使用 Python 内置 remove() 方法,从数组中间移除一个数据元素。
Here, we remove a data element at the middle of the array using the python in-built remove() method.
from array import *
array1 = array('i', [10,20,30,40,50])
array1.remove(40)
for x in array1:
print(x)
当我们编译并执行上述程序时,它会产生以下结果,显示该元素已从数组中移除。
When we compile and execute the above program, it produces the following result which shows the element is removed form the array.
10
20
30
50
= 搜索操作[id="_search_operation"] === 搜索操作
=== Search Operation
您可以在数组上执行搜索操作,以根据元素值或索引查找数组元素。
You can perform a search operation on an array to find an array element based on its value or its index.
= 示例[id="_example"]====示例
==== Example
在此,我们使用 Python 内置的 index() 方法搜索数据元素:
Here, we search a data element using the python in-built index() method −
from array import *
array1 = array('i', [10,20,30,40,50])
print (array1.index(40))
当我们编译并执行上述程序时,它将显示所搜索元素的索引。如果该值未出现在数组中,它将返回一个错误。
When we compile and execute the above program, it will display the index of the searched element. If the value is not present in the array, it will return an error.
3
= 更新操作[id="_update_operation"] === 更新操作
=== Update Operation
更新操作指的是在给定索引下更新数组中的现有元素。在此,我们只需为我们想要更新的目标索引重新分配一个新值。
Update operation refers to updating an existing element from the array at a given index. Here, we simply reassign a new value to the desired index we want to update.
= 示例[id="_example"]====示例
==== Example
在此示例中,我们更新索引 2 处的数组元素的值。
In this example, we are updating the value of array element at index 2.
from array import *
array1 = array('i', [10,20,30,40,50])
array1[2] = 80
for x in array1:
print(x)
在执行上述程序后,它会产生以下结果,显示索引位置 2 处的该新值。
On executing the above program, it produces the following result which shows the new value at the index position 2.
10
20
80
40
50