Numpy 简明教程

NumPy - Indexing & Slicing

ndarray 对象的内容可以通过索引或切片来访问和修改,就像 Python 的内置容器对象一样。

Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python’s in-built container objects.

如前所述,ndarray 对象中的项目遵循从 0 开始的索引。有三种类型的索引方法,即: field access, basic slicingadvanced indexing

As mentioned earlier, items in ndarray object follows zero-based index. Three types of indexing methods are available − field access, basic slicing and advanced indexing.

基本切片是 Python 在 n 维中对基本切片概念的扩展。通过将 start, stopstep 参数传递给内置 slice 函数来构建 Python 切片对象。将此切片对象传给数组以提取数组的一部分。

Basic slicing is an extension of Python’s basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.

Example 1

import numpy as np
a = np.arange(10)
s = slice(2,7,2)
print a[s]

它的输出如下:

Its output is as follows −

[2  4  6]

在上面的示例中,一个 ndarray 通过函数 arange() 准备对象。然后定义了一个切片对象,其中 start、stop 和 step 值分别为 2、7 和 2。当此切片对象被传递给 ndarray 时,它的一部分从索引 2 开始,到 7 结束,步长为 2,被切片。

In the above example, an ndarray object is prepared by arange() function. Then a slice object is defined with start, stop, and step values 2, 7, and 2 respectively. When this slice object is passed to the ndarray, a part of it starting with index 2 up to 7 with a step of 2 is sliced.

还可以通过将用冒号分隔的切片参数 (:start:stop:step) 直接传递给 ndarray 对象来获得相同的结果。

The same result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step) directly to the ndarray object.

Example 2

import numpy as np
a = np.arange(10)
b = a[2:7:2]
print b

在这里,我们将获得相同的输出 -

Here, we will get the same output −

[2  4  6]

如果只放一个参数,则将返回与索引对应的单个项目。如果在它的前面插入一个 :,将提取从该索引开始的所有项目。如果使用两个参数(它们之间带有 :),则切片两个索引之间的项目(不包括 stop 索引),默认步长为一。

If only one parameter is put, a single item corresponding to the index will be returned. If a : is inserted in front of it, all items from that index onwards will be extracted. If two parameters (with : between them) is used, items between the two indexes (not including the stop index) with default step one are sliced.

Example 3

# slice single item
import numpy as np

a = np.arange(10)
b = a[5]
print b

它的输出如下:

Its output is as follows −

5

Example 4

# slice items starting from index
import numpy as np
a = np.arange(10)
print a[2:]

现在,输出应为 −

Now, the output would be −

[2  3  4  5  6  7  8  9]

Example 5

# slice items between indexes
import numpy as np
a = np.arange(10)
print a[2:5]

在此处,输出将为 −

Here, the output would be −

[2  3  4]

上述描述也适用于多维数组 ndarray

The above description applies to multi-dimensional ndarray too.

Example 6

import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print a

# slice items starting from index
print 'Now we will slice the array from the index a[1:]'
print a[1:]

输出如下 −

The output is as follows −

[[1 2 3]
 [3 4 5]
 [4 5 6]]

Now we will slice the array from the index a[1:]
[[3 4 5]
 [4 5 6]]

切片还可以包含省略号 (…​),以生成与数组维度长度相同的选取元组。如果省略号用于行位置,它将返回一个包含行中项目的 ndarray。

Slicing can also include ellipsis (…) to make a selection tuple of the same length as the dimension of an array. If ellipsis is used at the row position, it will return an ndarray comprising of items in rows.

Example 7

# array to begin with
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])

print 'Our array is:'
print a
print '\n'

# this returns array of items in the second column
print 'The items in the second column are:'
print a[...,1]
print '\n'

# Now we will slice all items from the second row
print 'The items in the second row are:'
print a[1,...]
print '\n'

# Now we will slice all items from column 1 onwards
print 'The items column 1 onwards are:'
print a[...,1:]

此程序的输出如下 -

The output of this program is as follows −

Our array is:
[[1 2 3]
 [3 4 5]
 [4 5 6]]

The items in the second column are:
[2 4 5]

The items in the second row are:
[3 4 5]

The items column 1 onwards are:
[[2 3]
 [4 5]
 [5 6]]