开始使用免费开始使用

二维 NumPy 数组的子集选择

如果您的二维 numpy 数组是规则结构,也就是说每一行和每一列的元素数量都固定,那么复杂的子集选择会变得非常简单。请看下面的代码,其中从一个列表的列表中提取了元素 "a""c"

# numpy
import numpy as np
np_x = np.array(x)
np_x[:, 0]

逗号前的索引表示行,逗号后的索引表示列。: 用于切片;在这个示例中,它告诉 Python 包含所有行。

本练习是课程的一部分

Python 入门

查看课程

练习说明

  • 打印 np_baseball 的第 50 行。
  • 新建变量 np_weight_lb,包含 np_baseball 的整个第二列。
  • 选取 np_baseball 中第 124 位棒球运动员的身高(第一列)并打印出来。

交互式实操练习

通过完成这段示例代码来试试这个练习。

import numpy as np

np_baseball = np.array(baseball)

# Print out the 50th row of np_baseball


# Select the entire second column of np_baseball: np_weight_lb


# Print out height of 124th player
编辑并运行代码