简体   繁体   English

获取数组的特定索引

[英]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.现在我想得到一个新的数组 X,其中的元素 l 作为索引。 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?在这种情况下,正确的 function 是什么? I don't want to use a for loop.我不想使用 for 循环。

You need to convert your list X into a numpy array before you can index it with a list of indices:您需要将列表X转换为 numpy 数组,然后才能使用索引列表对其进行索引:

>>> X = np.array(X)[l]
>>> X
array([ 1000, 10000])

You should convert list X to numpy array您应该将列表 X 转换为 numpy 数组

x=np.array(X)

and then use indexing然后使用索引

x=x[x>100]

This will get the result you need without using where() function这将在不使用 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. “x>100”表达式创建一个boolean数组,其中True元素对应于x数组中大于100的元素。这个数组可以作为索引来提取满足条件的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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