Numpy 数组是 Python List 的绝佳替代品。Numpy 数组的一些主要优点是它们快速、易于使用,并让用户有机会跨整个阵列执行计算。
在以下示例中,首先创建两个 Python 列表。然后,导入 numpy 包并从新创建的列表中创建 numpy 数组。
代码演示:
# Create 2 new lists height and weight
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
# Import the numpy package as np
import numpy as np
# Create 2 numpy arrays from height and weight
np_height = np.array(height)
np_weight = np.array(weight)