開始使用免費開始

提取 NumPy 陣列子集

子集提取操作(也就是在串列或陣列後面使用方括號來取值)在串列和陣列中的用法完全一樣。

本練習已在後台載入 height_inweight_lb 這兩個串列。它們是普通串列,其中存放著 MLB 球員的身高和體重。練習還為你準備好了兩個 numpy 陣列:np_weight_lbnp_height_in

本練習屬於課程

Python 入門

檢視課程

練習說明

  • 透過印出索引為 50 的元素來從 np_weight_lb 中提取子集。
  • 印出 np_height_in 的一個子陣列,其中包含從索引 100 到 110(包含索引 110)的元素。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

import numpy as np

np_weight_lb = np.array(weight_lb)
np_height_in = np.array(height_in)

# Print out the weight at index 50


# Print out sub-array of np_height_in: index 100 up to and including index 110
編輯並執行程式碼