Convert list to numpy.ndarray

524 ワード

l_2d = [[0, 1, 2], [3, 4, 5]]

arr_2d = np.array(l_2d)

print(arr_2d)

# [[0 1 2]
#  [3 4 5]]
source: numpy_ndarray_list.py
Multi-dimensional list are just nested lists (list of lists), so it doesn't matter if the number of elements in the list doesn't match.
However, passing it to numpy.array() creates numpy.ndarray whose elements are built-in list.
単純なリストが1つしかない場合はnp.endarray()を使用して、リストをendarrayに簡単に変換できます.ただし、単一のリストではなくネストされたリストであればnp.endarrayに変換するにはarray()を使用します.