James Huang
James Huang

Reputation: 876

Numpy restrict array to given indices

Let's say I have an array J and another array O of indices that I would like to restrict J to. If J was [1, 0, 9, 1] and O was [0, 3], something would happen to give me [1, 1]. Is there any numpy function for this?

Upvotes: 1

Views: 316

Answers (1)

Nguyễn Minh Hiếu
Nguyễn Minh Hiếu

Reputation: 677

You can slice numpy array by selected indexes:

import numpy as np

arr = np.array([1, 0, 9, 1])
arr = arr[[0, 3]]  # >> array([1, 1])

Upvotes: 1

Related Questions