简体   繁体   中英

Get specific indices of Array

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM