【发布时间】:2019-04-04 00:25:13
【问题描述】:
我是 Python 新手,我正在尝试获取一行中的第一个元素并打印它。我该怎么做?
例如,数组 A 有:
([[ 0, 435, 500, 432, 658]])
我需要打印 0。我怎样才能做到这一点?
【问题讨论】:
我是 Python 新手,我正在尝试获取一行中的第一个元素并打印它。我该怎么做?
例如,数组 A 有:
([[ 0, 435, 500, 432, 658]])
我需要打印 0。我怎样才能做到这一点?
【问题讨论】:
这是一个只有 1 行的二维数组的示例,因此您可以使用以下 [0,0]
第一个 0 将选择第一行或一维数组,第二个 0 将选择该数组中的第一个元素。 更多关于索引 numpy 数组的信息可以在这里找到:https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html
arr=np.array([[ 0, 435, 500, 432, 658]])
#To get the first element from the array inside the first array, you should do
print(arr[0,0])
【讨论】:
[[0, 435, 500, 432, 658]][0][0]