python - 多维切片数组Python

标签 python arrays numpy slice

我想“选择”2D-np.array 的特定行和列,但不知道如何有效地执行此操作。

import numpy as np 

array = np.random.rand(4*100,4*360)

lon_min = np.array([-72.5, -72.5, -70, -67.5, -65, -62.5, -60, -57.5, -55, -52.5, -50])
lon_max = lon_min + 2.5
lat_min = np.array([-10, -7.5, -5, -4, -3, -3, -2, -1, -1, -1, -1])
lat_max = lat_min + 2.5 
indices_lon_min = (180+lon_min)*4
indices_lat_min = (50-lat_min)*4
indices_lon_max = indices_lon_min + 4*2.5
indices_lat_max = indices_lat_min -4*2.5
> indices_lat_min = [240. 230. 220. 216. 212. 212. 208. 204. 204. 204. 204.]
> indices_lat_max = [230. 220. 210. 206. 202. 202. 198. 194. 194. 194. 194.]
> indices_lon_min = [430. 430. 440. 450. 460. 470. 480. 490. 500. 510. 520.]
> indices_lon_max = [440. 440. 450. 460. 470. 480. 490. 500. 510. 520. 530.]

我希望有这样的东西:

array[indices_lat_min.astype(int): indices_lat_max.astype(int), indices_lon_min.astype(int):indices_lon_max.astype(int)]

基本上我要实现

array[240:230, 430:440]

但有多个“切片”。

最佳答案

也许是这样的:

array[list(map(range, indices_lat_min.astype(int), indices_lat_max.astype(int))),
      list(map(range, indices_lon_min.astype(int), indices_lon_max.astype(int)))]

这基本上是您的想法,我只是使用 map 获取索引列表中的所有最小最大对。

对于递减范围:

array[list(map(range, indices_lat_min.astype(int), indices_lat_max.astype(int), np.full(indices_lat_max.size, -1))),
      list(map(range, indices_lon_min.astype(int), indices_lon_max.astype(int),))]

关于python - 多维切片数组Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59765986/

相关文章:

c++ - int (*p)[3] 和 int *p[3] 有什么区别?

c - 如何将值作为动态数组从 vb 传递到 c

python - 你能让 numpy 除以 0 给出错误而不是警告吗?

python - 当 python 请求结束时切换到服务器上的另一个 IP

android - 如何连续播放原始文件夹中的歌曲,并且不循环播放

python - 在 numpy ndarray 中沿指定轴循环向量的有效方法是什么?

从 numpy 数组列表创建 numpy 数组的 Pythonic 方法

python - 我应该如何评估浮点 numpy 矩阵的等价性?

python - 如何从 Python 中的 OLS 结果中获取因变量的信息?

python - 无法处理一些关键的链接