ITと雑記とド田舎と

ド田舎在住エンジニアがIT備忘録と雑記を書くブログです

python3 numpy配列と組み込みリストの相互変換

pytrhon3 覚え書きNo.4です。

 

ものすごく基本なんですが、ちょっとやらないとすぐ忘れるので自戒をこめて記録。

numpyの配列と組み込みのlistの相互変換です。

 

import numpy as np

l1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(l1)
print('----------------------------------')
npl1 = np.array(l1)
print(npl1)
print('----------------------------------')
l2 = npl1.tolist()
print(l2)

実行結果
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
----------------------------------
[[1 2 3]
 [4 5 6]
 [7 8 9]]
----------------------------------
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

numpy.array(リスト)とnumpy配列.tolist()で終わり。シンプルですね。