Python Data Structure 简明教程

Python - Arrays

数组是一个容器,它可以保存一个固定数量的项,并且这些项应该属于同一种类型。大多数数据结构利用数组来实现其算法。以下是了解数组概念的重要术语:

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array are as follows −

  1. Element − Each item stored in an array is called an element.

  2. Index − Each location of an element in an array has a numerical index, which is used to identify the element.

Array Representation

数组可以在不同的语言中以不同的方式声明。下面是一个说明。

Arrays can be declared in various ways in different languages. Below is an illustration.

array declaration
array representation

根据上述插图,以下是要考虑的重要要点:

As per the above illustration, following are the important points to be considered −

  1. Index starts with 0.

  2. Array length is 10, which means it can store 10 elements.

  3. Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.

Basic Operations

数组支持的基本操作如下所述:

The basic operations supported by an array are as stated below −

  1. Traverse − print all the array elements one by one.

  2. Insertion − Adds an element at the given index.

  3. Deletion − Deletes an element at the given index.

  4. Search − Searches an element using the given index or by the value.

  5. Update − Updates an element at the given index.

通过将 array 模块导入 python 程序来在 Python 中创建数组。然后,声明数组如下所示:

Array is created in Python by importing array module to the python program. Then, the array is declared as shown below −

from array import *

arrayName = array(typecode, [Initializers])

类型码是用于定义数组将保存的值类型的代码。一些常用的类型码如下:

Typecode are the codes that are used to define the type of value the array will hold. Some common typecodes used are as follows −

Typecode

Value

b

Represents signed integer of size 1 byte

B

Represents unsigned integer of size 1 byte

c

Represents character of size 1 byte

i

Represents signed integer of size 2 bytes

I

Represents unsigned integer of size 2 bytes

f

Represents floating point of size 4 bytes

d

Represents floating point of size 8 bytes

在查看各种数组操作之前,让我们使用python创建一个数组并打印它。

Before looking at various array operations lets create and print an array using python.

Example

以下代码创建一个名为 array1 的数组。

The below code creates an array named array1.

from array import *

array1 = array('i', [10,20,30,40,50])

for x in array1:
   print(x)

Output

当我们编译和执行以上程序时,它会产生以下结果 -

When we compile and execute the above program, it produces the following result −

10
20
30
40
50

Accessing Array Element

我们可以使用元素的索引访问数组的每个元素。以下代码显示如何访问数组元素。

We can access each element of an array using the index of the element. The below code shows how to access an array element.

Example

from array import *

array1 = array('i', [10,20,30,40,50])

print (array1[0])

print (array1[2])

Output

当我们编译并执行上述程序时,它会产生以下结果,该结果显示元素插入在索引位置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
30

Insertion Operation

插入操作是将一个或多个数据元素插入数组中。根据要求,可以在数组的开头、结尾或任何给定的索引处添加新元素。

Insert operation is to 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.

Output

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.

Example

在此,我们使用 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)

Output

当我们编译并执行上述程序时,它会产生以下结果,显示该元素已从数组中移除。

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

Search Operation

您可以根据数组元素的值或其索引对其进行搜索。

You can perform a search for an array element based on its value or its index.

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))

Output

当我们编译并执行上述程序时,它会产生以下结果,该结果显示元素的索引。如果该值不存在于数组中,则程序将返回错误。

When we compile and execute the above program, it produces the following result which shows the index of the element. If the value is not present in the array then th eprogram returns an error.

3

Update Operation

更新操作是指在给定索引处从数组中更新现有元素。

Update operation refers to updating an existing element from the array at a given index.

Example

在这里,我们只需为我们想要更新的目标索引重新指定一个新值。

Here, we simply reassign a new value to the desired index we want to update.

from array import *

array1 = array('i', [10,20,30,40,50])

array1[2] = 80

for x in array1:
   print(x)

Output

当我们编译并执行上述程序时,它会产生以下结果,该结果显示索引位置2处的新值。

When we compile and execute the above program, it produces the following result which shows the new value at the index position 2.

10
20
80
40
50