Can NumPy array contain list?

Convert numpy.ndarray and list to each other

Posted: 2019-11-06 / Tags: Python, NumPy, List
Tweet

The NumPy array numpy.ndarray and the Python built-in type list can be converted to each other.

  • Convert list to numpy.ndarray: numpy.array[]
  • Convert numpy.ndarray to list: tolist[]

For convenience, the term "convert" is used, but in reality, a new object is generated while keeping the original object.

Sponsored Link

Convert list to numpy.ndarray: numpy.array[]

By passing list to numpy.array[], numpy.ndarray is generated based on it.

import numpy as np l_1d = [0, 1, 2] arr_1d = np.array[l_1d] print[arr_1d] print[arr_1d.dtype] # [0 1 2] # int64
source: numpy_ndarray_list.py

The data type dtype of generated numpy.ndarray is automatically determined from the original list, but can also be specified with the argument dtype.

See the following article for more information about the data type dtype in NumPy.

  • NumPy: Cast ndarray to a specific dtype with astype[]
arr_1d_f = np.array[l_1d, dtype=float] print[arr_1d_f] print[arr_1d_f.dtype] # [0. 1. 2.] # float64
source: numpy_ndarray_list.py

The same applies to multi-dimensional arrays of two or more dimensions.

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 list [list of list], 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.

Note that missing elements cannot be filled.

l_2d_error = [[0, 1, 2], [3, 4]] arr_2d_error = np.array[l_2d_error] print[arr_2d_error] # [list[[0, 1, 2]] list[[3, 4]]] print[arr_2d_error.dtype] # object print[arr_2d_error.shape] # [2,]
source: numpy_ndarray_list.py

Convert numpy.ndarray to list: tolist[]

The method tolist[] of numpy.ndarray returns list.

Depending on the number of dimensions of the original numpy.ndarray, a nested list is generated. Each element can be accessed by repeating the index [n].

1D:

arr_1d = np.arange[3] print[arr_1d] # [0 1 2] l_1d = arr_1d.tolist[] print[l_1d] # [0, 1, 2]
source: numpy_ndarray_list.py

2D:

arr_2d = np.arange[6].reshape[[2, 3]] print[arr_2d] # [[0 1 2] # [3 4 5]] l_2d = arr_2d.tolist[] print[l_2d] # [[0, 1, 2], [3, 4, 5]]
source: numpy_ndarray_list.py

3D:

arr_3d = np.arange[24].reshape[[2, 3, 4]] print[arr_3d] # [[[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] # [[12 13 14 15] # [16 17 18 19] # [20 21 22 23]]] l_3d = arr_3d.tolist[] print[l_3d] # [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]] print[l_3d[0]] # [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] print[l_3d[0][0]] # [0, 1, 2, 3] print[l_3d[0][0][0]] # 0
source: numpy_ndarray_list.py
Sponsored Link
Share
Tweet

Related Categories

  • Python
  • NumPy
  • List

Related Articles

  • Convert 1D array to 2D array in Python [numpy.ndarray, list]
  • Remove / extract duplicate elements from list in Python
  • NumPy: Remove rows / columns with missing value [NaN] in ndarray
  • numpy.delete[]: Delete rows and columns of ndarray
  • NumPy: Arrange ndarray in tiles with np.tile[]
  • Convert a list of strings and a list of numbers to each other in Python
  • Count elements from a list with collections.Counter in Python
  • Alpha blending and masking of images with Python, OpenCV, NumPy
  • Pretty-print with pprint in Python
  • NumPy: Rotate array [np.rot90]
  • Image processing with Python, NumPy
  • Generate gradient image with Python, NumPy
  • Extract, replace, convert elements of a list in Python
  • NumPy: Slicing ndarray
  • NumPy: How to use reshape[] and the meaning of -1

Video liên quan

Chủ Đề