Pythonの標準ライブラリーには配列がなく、listかtupleになる。
listは文字通りリストな上、なんでもいっしょくたに放り込める。
見た目はn次元配列に見えるようなこともできるが、実質は(1次元の)リストで、そのリストのオブジェクトとしてリストを入れ子にできる。
thisIsList = [0, 1, 2, 'a', 'b', [0, 1]] # it can contain any, including the other list print(thisIsList) thisIsList.append('aho') print(thisIsList)
Pythonの特徴的な配列アクセス方法にsliceがある。
start:stop:stepを指定してリストの要素にアクセスできる、stepは省略可能。
print(thisIsList[0:3]) print(thisIsList[::3]) print(thisIsList[5][1])
arrayライブラリーが配列を提供するが、こいつは一次元配列のみ。
thisIsArray = array.array('f', [0.0, 1.0, 2.0, 3.0, 4.0]) # it can contain the same data type, and is one dimensional print(thisIsArray)
numpyに含まれるndarrayが強烈。
"nd"の名の通りn次元配列が作れる。
thisIsNdarray = np.array([[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]], dtype = float) #it can contain same data type, and it can be N dimensional print(thisIsNdarray)
要素数さえ合えば変形も自由自在
thisIsNdarray = thisIsNdarray.reshape(4,3) # change to 3 x 4 matrix print(thisIsNdarray) thisIsNdarray = thisIsNdarray.reshape(2,2,3) # change to 3 x 2 x 2 matrix print(thisIsNdarray) thisIsNdarray = thisIsNdarray.reshape(3,4) # change back to 4 x 3 matrix print(thisIsNdarray)
sliceも使える、そして該当する要素に一括して代入可能。
print(thisIsNdarray[1, :]) # get index = 1 row of any column print(thisIsNdarray[:, 2]) # get index = 2 column of any row print(thisIsNdarray[:,0:3:2]) # get index 0 and 2 columns of any row thisIsNdarray[:,:] = 0.0 # set all to 0 print(thisIsNdarray) thisIsNdarray[0:2,:] = 1.0 #set index 1 and 2 row of any column to 1.0 print(thisIsNdarray) thisIsNdarray[:,2] = 2.0 # set index 2 column of any row to 2.0 print(thisIsNdarray)
初期化はこんな感じで。
thisIsNdarray = np.arange(0.0, 12.0, 1.0, dtype = float).reshape(3,4) # does same as the first initialization print(thisIsNdarray) thisIsNdarray = np.zeros((3, 4), dtype = float) # if no default value is necessary, 'empty' method can be used print(thisIsNdarray)