Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Integer Array Indexing | Indexing and Slicing
Ultimate NumPy
course content

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

Integer Array Indexing

Apart from basic indexing, where we use an integer for a single index, NumPy also allows us to use an entire 1D array of integers (a list of integers is also possible) for indexing. There's nothing complicated about it.

Each element of the integer array used for indexing is treated as an index, so, for example, array[[0, 1, 3]] retrieves elements at indices 0, 1, and 3 in the form of a 1D array, given that array is a 1D array itself. You can also use NumPy arrays for indexing, but it makes the code more cumbersome.

Let's look at an example:

123456789
import numpy as np array = np.array([23, 41, 7, 80, 3]) # Retrieving elements at indices 0, 1 and 3 print(array[[0, 1, 3]]) print('-' * 10) # Retrieving elements at indices 1, -1 and 2 in this order print(array[[1, -1, 2]]) # IndexError is thrown since index 5 is out of bounds print(array[[2, 5]])
copy

Here is an illustration to make things clear:

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 5
toggle bottom row

Integer Array Indexing

Apart from basic indexing, where we use an integer for a single index, NumPy also allows us to use an entire 1D array of integers (a list of integers is also possible) for indexing. There's nothing complicated about it.

Each element of the integer array used for indexing is treated as an index, so, for example, array[[0, 1, 3]] retrieves elements at indices 0, 1, and 3 in the form of a 1D array, given that array is a 1D array itself. You can also use NumPy arrays for indexing, but it makes the code more cumbersome.

Let's look at an example:

123456789
import numpy as np array = np.array([23, 41, 7, 80, 3]) # Retrieving elements at indices 0, 1 and 3 print(array[[0, 1, 3]]) print('-' * 10) # Retrieving elements at indices 1, -1 and 2 in this order print(array[[1, -1, 2]]) # IndexError is thrown since index 5 is out of bounds print(array[[2, 5]])
copy

Here is an illustration to make things clear:

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 5
toggle bottom row

Integer Array Indexing

Apart from basic indexing, where we use an integer for a single index, NumPy also allows us to use an entire 1D array of integers (a list of integers is also possible) for indexing. There's nothing complicated about it.

Each element of the integer array used for indexing is treated as an index, so, for example, array[[0, 1, 3]] retrieves elements at indices 0, 1, and 3 in the form of a 1D array, given that array is a 1D array itself. You can also use NumPy arrays for indexing, but it makes the code more cumbersome.

Let's look at an example:

123456789
import numpy as np array = np.array([23, 41, 7, 80, 3]) # Retrieving elements at indices 0, 1 and 3 print(array[[0, 1, 3]]) print('-' * 10) # Retrieving elements at indices 1, -1 and 2 in this order print(array[[1, -1, 2]]) # IndexError is thrown since index 5 is out of bounds print(array[[2, 5]])
copy

Here is an illustration to make things clear:

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Apart from basic indexing, where we use an integer for a single index, NumPy also allows us to use an entire 1D array of integers (a list of integers is also possible) for indexing. There's nothing complicated about it.

Each element of the integer array used for indexing is treated as an index, so, for example, array[[0, 1, 3]] retrieves elements at indices 0, 1, and 3 in the form of a 1D array, given that array is a 1D array itself. You can also use NumPy arrays for indexing, but it makes the code more cumbersome.

Let's look at an example:

123456789
import numpy as np array = np.array([23, 41, 7, 80, 3]) # Retrieving elements at indices 0, 1 and 3 print(array[[0, 1, 3]]) print('-' * 10) # Retrieving elements at indices 1, -1 and 2 in this order print(array[[1, -1, 2]]) # IndexError is thrown since index 5 is out of bounds print(array[[2, 5]])
copy

Here is an illustration to make things clear:

Tarefa

You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures (use a negative index only for the last element).

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 5
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt