Kinh Nghiệm Hướng dẫn Is it possible to convert the NumPy array to list in Python? Chi Tiết
Bạn đang tìm kiếm từ khóa Is it possible to convert the NumPy array to list in Python? được Update vào lúc : 2022-12-06 01:05:09 . Với phương châm chia sẻ Mẹo về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.
In this tutorial, Ill show you how to convert from Numpy array to list. Essentially, well take a Numpy array and convert it to a Python list using the tolist() method.
Nội dung chính
- A quick introduction Numpy tolist
- The tolist method converts from Numpy array to Python list
- The syntax of the tolist method
- The Output of tolist
- Examples: How to Convert from a Numpy array to a Python List
- EXAMPLE 1: Convert a 1-dimensional array to a list
- EXAMPLE 2: Convert a 2-dimensional array to a nested list
- Join our course to learn more about Numpy
- Introduction
- Syntax
- Examples
- Frequently Asked Questions
- Numpy random normal
- the Numpy random randint function
- the Numpy arange function
- Convert a 1-dimensional array to a list
- Convert a 2-dimensional array to a nested list
- How to create Numpy arrays
- How to use the Numpy random functions
- What Numpy axes are, and how to use them
- What the Numpy random seed function does
- How to reshape, split, and combine your Numpy arrays
- and much more
In the tutorial, Ill give you a quick introduction, Ill explain the syntax, and Ill show you a couple of simple examples of this technique.
If you need something specific, you can click on any of the following links, and the link will take you directly to the appropriate location in the post.
Table of Contents:
Ok. Lets get into it.
A quick introduction Numpy tolist
Lets start with a quick introduction to the technique.
If youre reading this post, youre probably already a little familiar with Numpy arrays and Python lists, but lets quickly recap.
Lists and Numpy arrays are special data structures that store data in Python programs.
In particular, Numpy arrays are a special structure for storing numeric data and working with Numeric data. They store numeric data in a row-and-column structure that looks something like this:
We typically use Numpy arrays quite a bit for data science, machine learning, and scientific computing. When working with numeric data in Numpy, we commonly keep the data in array form, but there are some instances where we need to convert the array to a Python list.
The tolist method converts from Numpy array to Python list
To accomplish this, we can use the tolist() method from Numpy.
The tolist() method takes a Numpy array and outputs a Python list.
Its really very simple.
Having said that, lets look the syntax.
The syntax of the tolist method
The syntax of the Numpy tolist() method is extremely simple. Its arguably one of the simplest Numpy techniques.
You start by typing the name of a Numpy array.
This is important. Since the tolist() technique is a method, you call it by typing the name of an object first.
After you type the name of your Numpy array, you use so-called dot syntax to call the method.
So you type a dot and then the name of the method, tolist().
Thats really it!
Obviously, this assumes that you already have a Numpy array of some kind.
For more information about how to generate different kinds of Numpy arrays, you might check out our tutorials about:
The Output of tolist
Once you call the tolist() method, the output will be a Python list.
If the input array is a 1-dimensional Numpy array, then the output will be a simple 1D list.
If the input array is 2-dimensional or multi-dimensional, then the output will be a nested list.
Additionally, the elements of the input will be converted to the closest built-in Python data types.
Examples: How to Convert from a Numpy array to a Python List
Ok. Now that youve seen how the syntax works, lets look a couple of simple examples.
Examples:
Lets start with example 1.
EXAMPLE 1: Convert a 1-dimensional array to a list
Here, were going to convert a simple 1-dimensional Numpy array to a Python list.
Create 1D Numpy Array
First, we need to create a 1-dimensional Numpy array.
To do this, well use the Numpy arange function to create a 1D array that contains a sequence of values.
my_1d_array = np.arange(start = 1, stop = 6)
And lets print it out to see the contents:
print(my_1d_array)
OUT:
[1 2 3 4 5]
Having said that, if we check the data type with type(my_1d_array), youll notice that my_1d_array is a numpy.ndarray.
Convert Array to List
Now, lets convert it to a list.
Here, well use the tolist() method and save the output to the name my_1d_list.
my_1d_list = my_1d_array.tolist()
And lets print it out:
print(my_1d_list)
OUT:
[1, 2, 3, 4, 5]
Additionally, if we check the data type with the code type(my_1d_list), youll see that my_1d_list is a list.
Explanation
This is really simple.
We typed the name of the Numpy array, and called the tolist() method using dot syntax.
The output, my_1d_list, essentially contains the same elements, but its a Python list instead of a Numpy array.
EXAMPLE 2: Convert a 2-dimensional array to a nested list
Next, wel convert a 2-dimensional Numpy array to a nested Python list.
Create 2D Numpy Array
First, we need to create the 2-dimensional Numpy array.
To do this, well use Numpy arange to create a sequence of values, and well use the Numpy reshape method to re-shape that 1D array into a 2D array.
my_2d_array = np.arange(start = 1, stop = 7).reshape(2,3)
And lets print it out to see the contents:
print(my_2d_array)
OUT:
[[1 2 3]
[4 5 6]]
And again, if we check the data type with type(my_2d_array), youll notice that my_2d_array is a numpy.ndarray.
Convert Array to List
Now, lets convert the array to a list.
Once again, well use the tolist() method. And well save the output to the name my_2d_list.
my_1d_list = my_1d_array.tolist()
And lets print it out:
print(my_2d_list)
OUT:
[[1, 2, 3], [4, 5, 6]]
If you check the data type with the code type(my_2d_list), youll see that my_2d_list is a Python list.
Explanation
Again, this is really simple.
To convert from a Numpy array to list, we simply typed the name of the 2D Numpy array, and then called the Numpy tolist() method which produced a Python list as an output.
Moreover, take a look the output list itself: [[1, 2, 3], [4, 5, 6]]
From the structure, we can see that this is a nested Python list. Two lists of 3 elements each, that exist within a larger list. Its a list-of-lists.
This is how the Numpy tolist method handles multi-dimensional input arrays. When tolist operates on a multi-dimensional input, it produces a nested array as an output.
Leave your other questions in the comments below
Do you have any other questions about the Numpy tolist method?
If so, leave your questions in the comments section near the bottom of the page.
Join our course to learn more about Numpy
Are you interested in learning more about Numpy?
This tutorial should have shown you how to use the Numpy tolist() method, but to master numeric data manipulation in Python, theres a lot more to learn.
If youre serious about learning Numpy, you should join our premium course, Numpy Mastery.
In this course, youll learn everything you need to know about Numpy.
Additionally, when you join, youll get access to our unique practice system.
This practice system will enable you to memorize all of the syntax that you learn. If you take this course and practice like we show you, youll be able to write Numpy code fluently, accurately, and 100% from memory.
Find out more here:
Learn More About Numpy Mastery
Reply
0
0
Chia sẻ
Chia Sẻ Link Tải Is it possible to convert the NumPy array to list in Python? miễn phí
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ề Clip Is it possible to convert the NumPy array to list in Python? tiên tiến và phát triển nhất và Chia SẻLink Download Is it possible to convert the NumPy array to list in Python? Free.
Giải đáp vướng mắc về Is it possible to convert the NumPy array to list in Python?
Nếu sau khi đọc nội dung bài viết Is it possible to convert the NumPy array to list in Python? 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
#convert #NumPy #array #list #Python