多次元配列: ndarray の生成

リストから numpy の多次元配列 (ndarray) を生成します.

プログラム例

import numpy as np

list_1D = [i**2 for i in range(5)]                # 1D list
list_2D = [[i+10*j for i in range(3)] for j in range(4)]   # 2D list

arr_1d  = np.array(list_1D)
arr_2d  = np.array(list_2D)

print(arr_1d)
print(arr_2d)

実行結果

[ 0  1  4  9 16]
[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]