import numpy as np
X = [-10000, -1000, -100, -10, -1, 0, 1, 10, 100, 1000, 10000]
l = np.where(np.array(X) > 100)[0]
So i have
l = array([ 9, 10], dtype=int64)
Now I want to get a new Array X with the elements of l as indices. I want to get:
X = [1000, 10000]
I thought of:
X = X[l]
But it does not work. What is the proper function to use in this case? I don't want to use a for loop.
CodePudding user response:
You need to convert your list X
into a numpy array before you can index it with a list of indices:
>>> X = np.array(X)[l]
>>> X
array([ 1000, 10000])
CodePudding user response:
You should convert list X to numpy array
x=np.array(X)
and then use indexing
x=x[x>100]
This will get the result you need without using where() function
"x>100" expression creates a boolean array with True elements corresponding to the elements of x array which are larger than 100. This array can be used as index to extract elements satisfying the condition.