在 ndarray python 中获取第一个维度

mas*_*ejo 6 python numpy multidimensional-array

假设我有一个像这样的 ndarray :

    a = [[20 43 61 41][92 23 43 33]]
Run Code Online (Sandbox Code Playgroud)

我想获取这个 ndarray 的第一个维度。所以我尝试这样的事情:

    a[0,:]
Run Code Online (Sandbox Code Playgroud)

我希望它会返回这样的结果:

    [[20 43 61 41]]
Run Code Online (Sandbox Code Playgroud)

但我收到了这个错误:

   TypeError: 'numpy.int32' object is not iterable
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?

fal*_*tru 2

使用切片:

>>> import numpy as np
>>> a = np.array([[20, 43, 61, 41], [92, 23, 43, 33]])
>>> a[:1]  # OR a[0:1]
array([[20, 43, 61, 41]])
>>> print(a[:1])
[[20 43 61 41]]
Run Code Online (Sandbox Code Playgroud)