Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

One-dimensional slicing and indexing


Slicing of one-dimensional NumPy arrays works just like slicing of Python lists. We can select a piece of an array from the index 3 to 7 that extracts the elements 3 through 6 (see the slicing1d.py file in the Chapter02 folder of this book's code bundle), as shown in the following code snippet:

In: a = arange(9)
In: a[3:7]
Out: array([3, 4, 5, 6])

We can select elements from the index 0 to 7 with a step of two, as shown in the following lines of code:

In: a[:7:2]
Out: array([0, 2, 4, 6])

Just as in Python, we can use negative indices and reverse the array, as shown in the following lines of code:

In: a[::-1]
Out: array([8, 7, 6, 5, 4, 3, 2, 1, 0])