Kinh Nghiệm Hướng dẫn Can NumPy array contain list? Chi Tiết
Ban đang tìm kiếm từ khóa Can NumPy array contain list? được Cập Nhật vào lúc : 2022-01-03 04:12:06 . Với phương châm chia sẻ Thủ Thuật Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi đọc nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Ad lý giải và hướng dẫn lại nha.
Convert numpy.ndarray and list to each other
Posted: 2022-11-06 / Tags: Python, NumPy, ListTweet
The NumPy array numpy.ndarray and the Python built-in type list can be converted to each other.
Nội dung chính
- Convert numpy.ndarray and list to each otherConvert list to numpy.ndarray: numpy.array()Convert numpy.ndarray to list: tolist()Related CategoriesRelated ArticlesVideo liên quan
- 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.pySponsored LinkShareTweet
Related Categories
- PythonNumPyList
Related Articles
- Convert 1D array to 2D array in Python (numpy.ndarray, list)Remove / extract duplicate elements from list in PythonNumPy: Remove rows / columns with missing value (NaN) in ndarraynumpy.delete(): Delete rows and columns of ndarrayNumPy: Arrange ndarray in tiles with np.tile()Convert a list of strings and a list of numbers to each other in PythonCount elements from a list with collections.Counter in PythonAlpha blending and masking of images with Python, OpenCV, NumPyPretty-print with pprint in PythonNumPy: Rotate array (np.rot90)Image processing with Python, NumPyGenerate gradient image with Python, NumPyExtract, replace, convert elements of a list in PythonNumPy: Slicing ndarrayNumPy: How to use reshape() and the meaning of -1
Reply
9
0
Chia sẻ
Clip Can NumPy array contain list? ?
Bạn vừa đọc Post Với Một số hướng dẫn một cách rõ ràng hơn về Review Can NumPy array contain list? tiên tiến và phát triển nhất
Share Link Download Can NumPy array contain list? miễn phí
Quý khách đang tìm một số trong những Chia Sẻ Link Down Can NumPy array contain list? miễn phí.
Giải đáp vướng mắc về Can NumPy array contain list?
Nếu Ban sau khi đọc nội dung bài viết Can NumPy array contain list? , bạn vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Admin lý giải và hướng dẫn lại nha
#NumPy #array #list