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

Зміст курсу

Ultimate NumPy

Ultimate NumPy

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

bookInteger 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:

Завдання

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).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
toggle bottom row

bookInteger 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:

Завдання

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).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
toggle bottom row

bookInteger 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:

Завдання

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).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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:

Завдання

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).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 5
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt