Python 简明教程
Python - Access Array Items
在 Python 中访问数组项是指在给定数组的特定索引处检索存储的值的过程。这里,索引是一个数值,它表示数组项的位置。因此,您可以使用此索引来访问 Python 中数组的元素。
Accessing an array items in Python refers to the process of retrieving the value stored at a specific index in the given array. Here, index is an numerical value that indicates the location of array items. Thus, you can use this index to access elements of an array in Python.
Accessing array items in Python
您可以使用以下方法在 Python 中访问数组项 −
You can use the following ways to access array items in Python −
-
Using indexing
-
Using iteration
-
Using enumerate() function
Using indexing
通过索引访问数组元素的过程称为索引。在这个过程中,我们只需要传递索引运算符 [] 内部的索引号。Python 中数组的索引从 0 开始,这意味着您可以在索引 0 处找到其第一个元素,在比给定数组的长度小 1 的索引处找到最后一个元素。
The process of accessing elements of an array through the index is known as Indexing. In this process, we simply need to pass the index number inside the index operator []. The index of an array in Python starts with 0 which means you can find its first element at index 0 and the last at one less than the length of given array.
Example
以下示例显示了如何使用索引访问数组元素。
The following example shows how to access elements of an array using indexing.
import array as arr
# creating array
numericArray = arr.array('i', [111, 211, 311, 411, 511])
#indexing
print (numericArray[0])
print (numericArray[1])
print (numericArray[2])
当您运行上述代码时,它将显示以下 output −
When you run the above code, it will show the following output −
111
211
311
Using iteration
在此方法中,使用 loops (如 for 和 while)重复执行代码块。当您希望逐个访问数组元素时,可以使用它。
In this approach, a block of code is executed repeatedely using loops such as for and while. It is used when you want to access array elements one by one.
Example
在下面的代码中,我们使用 for 循环访问指定数组的所有元素。
In the below code, we use the for loop to access all the elements of the specified array.
import array as arr
# creating array
numericArray = arr.array('i', [111, 211, 311, 411, 511])
# iteration through for loop
for item in numericArray:
print(item)
在执行上述代码后,它将显示以下结果 −
On executing the above code, it will display the following result −
111
211
311
411
511
Using enumerate() function
enumerate() function 可以用来访问数组元素,它接受一个数组和一个可选的起始索引作为参数值,并通过迭代返回数组项。
The enumerate() function can be used to access elements of an array. It accepts an array and an optional starting index as parameter values and returns the array items by iterating.
Example
在下面的示例中,我们将看到如何使用 enumerate() 函数来访问数组项。
In the below example, we will see how to use the enumerate() function to access array items.
import array as arr
# creating array
numericArray = arr.array('i', [111, 211, 311, 411, 511])
# use of enumerate() function
for loc, val in enumerate(numericArray):
print(f"Index: {loc}, value: {val}")
它将生成以下 output −
It will produce the following output −
Index: 0, value: 111
Index: 1, value: 211
Index: 2, value: 311
Index: 3, value: 411
Index: 4, value: 511
Accessing a range of array items in Python
在 Python 中,要访问一系列数组项,可以使用使用索引运算符 [] 和冒号 (:) 执行的分片操作。
In Python, to access a range of array items, you can use the slicing operation which is performed using index operator [] and colon (:).
此操作使用多种格式实现,如下所列:
This operation is implemented using multiple formats, which are listed below −
-
Use the [:index] format to access elements from beginning to desired range.
-
To access array items from end, use [:-index] format.
-
Use the [index:] format to access array items from specific index number till the end.
-
Use the [start index : end index] to slice the array elements within a range. You can also pass an optional argument after end index to determine the increment between each index.
Example
以下示例演示了 Python 中的分片操作。
The following example demonstrates the slicing operation in Python.
import array as arr
# creating array
numericArray = arr.array('i', [111, 211, 311, 411, 511])
# slicing operation
print (numericArray[2:])
print (numericArray[0:3])
在执行上述代码后,它将显示以下结果 −
On executing the above code, it will display the following result −
array('i', [311, 411, 511])
array('i', [111, 211, 311])