Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to get 3 elements in a string/list/array, the first two being consecutive and the third separated from them by a step?

Given the array:

import numpy as np
arr = np.array(range(10))
print(arr)
[0,1,2,3,4,5,6,7,8,9]

is it possible to get in one single slice ex. arr[1:6:1] the numbers [1, 2, 5]?

I’ve tried everything, from steps to multiple slicing, it seems I cannot get past the uneven step between the numbers. I’m pretty sure it’s not possible, but if it is, I’d like to hear it since I wasted a few hours on this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

In numpy, you can directly pass a list of indeces instead of a slice object:

arr[[1, 2, 5]]

If there is an underlying pattern to those indeces you can use it to create the index list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading