Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python extract elements from array

Tags:

python

numpy

I have an 8000-element 1D array.

I want to obtain the following two arrays:

  1. test contains the element with the index from [1995:1999], [3995:3999], [5999:5999], [7995:7999].

  2. train should contains everything else.

How should I do that?


idx = [1995,1996,1997,1998, 1999, 3995, 3996, 3997,3998, 3999, 5995, 5996, 5997, 5998, 5999, 7995, 7996, 7997, 7998, 7999]
test = [X[i] for i in idx]

train = [X[i] for i **not** in idx]
like image 339
wrek Avatar asked Dec 18 '22 07:12

wrek


1 Answers

Based on your example, a simple workaround would be this:

train = [X[i] for i, _ in enumerate(X) if i not in idx]
like image 125
Arya McCarthy Avatar answered Dec 21 '22 10:12

Arya McCarthy