Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按条件索引大型2d numpy数组_Python_Arrays_Numpy_Indexing - Fatal编程技术网

Python 按条件索引大型2d numpy数组

Python 按条件索引大型2d numpy数组,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我有一个形状为(15003712)的2D NumPy数组。我想找到数组的索引,它们的值介于-10和-40之间。到目前为止,我已经: 对于lon_数组中的项: 对于项目中的值: 如果value>=-40和value如果lonu数组是一个列表列表(Python的内置基本数据类型),使用enumerate(…)将是了解元素索引的最佳方法: for y, row in enumerate(lon_array): for x, value in enumerate(row): if

我有一个形状为(15003712)的2D NumPy数组。我想找到数组的索引,它们的值介于-10和-40之间。到目前为止,我已经:

对于lon_数组中的项: 对于项目中的值:
如果value>=-40和value如果
lonu数组
是一个列表列表(Python的内置基本数据类型),使用
enumerate(…)
将是了解元素索引的最佳方法:

for y, row in enumerate(lon_array):
    for x, value in enumerate(row):
        if -40 <= value <= -10:
            index = (y, x)
            # do something useful with it...
对于y,枚举(lon_数组)中的行:
对于x,枚举(行)中的值:

如果-40if
lonu array
是一个列表列表(Python的内置基本数据类型),那么使用
enumerate(…)
将是了解元素索引的最佳方法:

for y, row in enumerate(lon_array):
    for x, value in enumerate(row):
        if -40 <= value <= -10:
            index = (y, x)
            # do something useful with it...
对于y,枚举(lon_数组)中的行:
对于x,枚举(行)中的值:

如果使用
numpy.where
的-40,您可以根据数据的值提取索引,并在构建
numpy
的优化
c code
中对数据执行迭代:

import numpy as np

x = np.random.random(10).reshape(2, 5)
print(x)
indices = np.where(x < 0.2)  #<--- this selects the indices based on a filter
print(indices)
x[indices]
因为过滤器中有两个条件,我建议您使用以下构造,它允许构建比
np更复杂的布尔表达式。其中
直接接受:

indices = np.where(np.logical_or(x < 0.2, x > 0.8))
index=np.where(np.logical_或(x<0.2,x>0.8))

使用
numpy。其中,
可以根据数据值提取索引,并在构建
numpy
的优化
c代码中对数据执行迭代:

import numpy as np

x = np.random.random(10).reshape(2, 5)
print(x)
indices = np.where(x < 0.2)  #<--- this selects the indices based on a filter
print(indices)
x[indices]
因为过滤器中有两个条件,我建议您使用以下构造,它允许构建比
np更复杂的布尔表达式。其中
直接接受:

indices = np.where(np.logical_or(x < 0.2, x > 0.8))
index=np.where(np.logical_或(x<0.2,x>0.8))

假设您的
lon\u数组是NumPy数组,您可以使用以下方法:

find_index=np.where(np.logical_和(lon_数组>=-40,lon_arr>>lon_arr
数组([[20,-40],
[ 30, -30],
[ 20, -14],
[ 30, -30]])

>>>np.asarray(np.where(np.logical_)和(lon_arr>=-40,lon_arr假设您的
lon_数组是NumPy数组,您可以使用以下方法:

find_index=np.where(np.logical_和(lon_数组>=-40,lon_arr>>lon_arr
数组([[20,-40],
[ 30, -30],
[ 20, -14],
[ 30, -30]])

>>>np.asarray(np.where)(np.logical_)和(lon_arr>=-40,lon_arr为了完整性,您可以使用作为其他答案中提出的基于
numpy的解决方案的替代方案:

[1245]中的
:lon_数组=np.linspace(-70,20,10)。重塑(2,5)
In[1246]:lon_阵列
出[1246]:
数组([-70.,-60.,-50.,-40.,-30.],
[-20., -10.,   0.,  10.,  20.]])

在[1247]:idx=np.nonzero(np.logical_)和(lon_数组>=-40,lon_数组中,为了完整性,您可以使用作为
numpy的替代方案。其中,其他答案中提出的基于
的解决方案:

[1245]中的
:lon_数组=np.linspace(-70,20,10)。重塑(2,5)
In[1246]:lon_阵列
出[1246]:
数组([-70.,-60.,-50.,-40.,-30.],
[-20., -10.,   0.,  10.,  20.]])

在[1247]中:idx=np.nonzero(np.logical_)和(lon_数组>=-40,lon_数组能否添加长度为5或6的2D数组?它是否一定是numpy数组?如果是,则添加标记。或者使用numpy只是试图找到匹配项和“数组”的索引是否真的是一个普通的列表?是否可以添加长度为5或6的2D数组?它是否一定是numpy数组?如果是,则添加标记。或者使用numpy只是试图查找匹配项的索引,而您的“数组”是否真的是一个普通列表?