python numpyの多次元配列の行数と列数を取得する

pythonで、ライブラリnumpyのshapeを使用して、多次元配列の行数と列数を取得するサンプルコードを記述してます。pythonのバージョンは3.8.5を使用してます。
環境
- OS windows10 pro 64bit
- python 3.8.5
numpyインストール
numpyをインストールされていない方は、pipでインストールしておきます。
pip install numpy
# Successfully installed numpy-1.19.4
shape使い方
shapeを使用すると、多次元配列の行数と列数を取得することが可能です。
import numpy as np
arr = np.array([["a", "b"], ["c", "d"]])
print(arr.shape)
# (2, 2)
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr.shape)
# (3, 3)
arr = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]])
print(arr.shape)
# (2, 2, 2)
# 1次元の場合は(3,)となります
arr = np.array([1, 2, 3])
print(arr.shape)
# (3,)
-
前の記事
React.js ライブラリ「react-iconly」を使ってアイコンを利用する 2021.06.04
-
次の記事
python PySimpleGUIのTableのヘッダーを設定する 2021.06.05
コメントを書く