Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Slicing and Indexing

NumPy Slicing and Indexing

The contents of an ndarray object can be accessed and modified through indexing or slicing, just like the slicing operation on lists in Python.

An ndarray can be indexed based on subscripts from 0 to n. A slice object can be created through the built-in slice function by setting the start, stop, and step parameters to cut a new array from the original array.

Example

import

numpy

as

np

a

=

np

.

arange

(

10

)

s

=

slice

(

2

,

7

,

2

)

Start from index 2 to index 7, with an interval of 2

print

(

a

[

s

]

)

The output is:

[2  4  6]

In the above example, we first create the ndarray object through the arange() function. Then we set the start, stop, and step parameters to 2, 7, and 2 respectively.

We can also separate slice parameters with a colon start:stop:step to perform the slicing operation:

Example

import

numpy

as

np

a

=

np

.

arange

(

10

)

b

=

a

[

2

:

7

:

2

]

Start from index 2 to index 7, with an interval of 2

print

(

b

)

The output is:

[2  4  6]

Explanation of the colon: if only one parameter is placed, such as [2], a single element corresponding to that index will be returned. If it is [2:], it means all items from that index onward will be extracted. If two parameters are used, such as [2:7], then the items between the two indices (not including the stop index) are extracted.

Example

import

numpy

as

np

a

=

np

.

arange

(

10

)

[0 1 2 3 4 5 6 7 8 9]

b

=

a

[

5

]

print

(

b

)

The output is:

5

Example

import

numpy

as

np

a

=

np

.

arange

(

10

)

print

(

a

[

2

:

]

)

The output is:

[2  3  4  5  6  7  8  9]

Example

import

numpy

as

np

a

=

np

.

arange

(

10

)

[0 1 2 3 4 5 6 7 8 9]

print

(

a

[

2

:

5

]

)

The output is:

[2  3  4]

The above index extraction method also applies to multidimensional arrays:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

3

,

4

,

5

]

,

[

4

,

5

,

6

]

]

)

print

(

a

)

Start slicing from a certain index

print

(

'

Start slicing from array index a[1:]

'

)

print

(

a

[

1

:

]

)

The output is:

[[1 2 3]

 [3 4 5]

 [4 5 6]]

从数组索引 a[1:] 处开始切割

[[3 4 5]

 [4 5 6]]

A slice can also include an ellipsis … to make the length of the selection tuple the same as the number of array dimensions. If an ellipsis is used in the row position, it returns an ndarray containing the elements in the row.

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

3

,

4

,

5

]

,

[

4

,

5

,

6

]

]

)

print

(

a

[

...,

1

]

)

Elements of the 2nd column

print

(

a

[

1

,...

]

)

Elements of the 2nd row

print

(

a

[

...,

1

:

]

)

The 2nd column and all remaining elements

The output is:



[2 4 5]

[3 4 5]

[[2 3]

 [4 5]

 [5 6]]