Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

One-dimensional slicing and indexing


Slicing of one-dimensional NumPy arrays works just like slicing of Python lists. Select a piece of an array from index 3 to 7 that extracts the elements 3 through 6:

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

Select elements from index 0 to 7 with step 2 as follows:

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

Similarly, as in Python, use negative indices and reverse the array with this code snippet:

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