多维数组的numpy高级索引

2024-03-28 08:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

假设x是一个3x3 numpy数组,它包含以下内容:

import numpy as np
x = np.array([[ 1.,  2.,  3.],
              [ 4.,  5.,  6.],
              [ 7.,  8.,  9.]])

是否有一些索引可以提供以下子数组:

^{2}$

Tags: importnumpyasnp数组array
2条回答

可以将integer array indexing与数组元组一起使用:

>>> rows = np.array([[0, 0],
...                  [1, 1]], dtype=np.intp)
>>> columns = np.array([[0, 1],
...                     [1, 2]], dtype=np.intp)
>>> x[rows, columns]
array([[ 1.,  2.],
       [ 5.,  6.]])

你可以用

x[:2,:2]

来解决你的问题

相关问题 更多 >