Home  >  Article  >  Backend Development  >  Convert numpy to list: Tips to improve data processing efficiency

Convert numpy to list: Tips to improve data processing efficiency

WBOY
WBOYOriginal
2024-01-19 10:11:16864browse

Convert numpy to list: Tips to improve data processing efficiency

In data processing, it is often necessary to convert numpy arrays into lists. Numpy arrays are very powerful data structures, but sometimes you need to use lists for further operations. At the same time, there are also some operations that require conversion between numpy arrays and lists. In this article, we will introduce the method of converting numpy array to list and provide specific code examples.

1. Use the tolist() method

The tolist() method is provided in numpy, which can simply convert numpy into a list. The following is an example:

import numpy as np

a = np.array([[1,2,3], [4,5,6]])
a_list = a.tolist()

print(a_list)

Output result:

[[1, 2, 3], [4, 5, 6]]

tolist() method is very simple, but relatively inefficient. If you need to handle larger arrays, the tolist() method can become very slow.

2. Use the cache method

If you want to improve efficiency when processing large numpy arrays, you can use the cache method. That is, add elements in numpy to the list one by one. The following is an example:

import numpy as np

a = np.array([[1,2,3], [4,5,6]])

# np.ndarray.flat 属性将返回一个迭代器,遍历数组中的所有元素
a_list = [item for item in a.flat]

print(a_list)

Output result:

[1, 2, 3, 4, 5, 6]

Using this method can avoid frequent conversion between numpy and list, improving efficiency.

3. Use the reshape method

The reshape method can reshape the numpy array into a shape similar to the list, and the list can be expanded by the flatten method. The following is an example:

import numpy as np

a = np.array([[1,2,3], [4,5,6]])
a_reshape = a.reshape(-1)
a_list = a_reshape.tolist()

print(a_list)

Output results:

[1, 2, 3, 4, 5, 6]

The reshape method can transform the array into a one-dimensional array, and then use the tolist() method to convert it into a list.

4. Use the list() method

Using the list() method can directly convert a numpy array into a list, but you need to pay attention to the dimensions of the array. This method only works if the dimension is 1.

import numpy as np

a = np.array([1,2,3])
a_list = list(a)

print(a_list)

Output result:

[1, 2, 3]

If the dimension of the array is not 1, you need to use other methods.

Summary

The above are several methods to convert numpy arrays into lists, among which the tolist() method is the most common method, but its efficiency is relatively low. When dealing with large arrays, using cache methods and reshape methods can improve efficiency. We need to choose the most suitable method according to our own needs.

Attach the complete code:

import numpy as np

# tolist()方法
a = np.array([[1,2,3], [4,5,6]])
a_list = a.tolist()
print(a_list)

# 缓存方法
a = np.array([[1,2,3], [4,5,6]])
a_list = [item for item in a.flat]
print(a_list)

# reshape方法
a = np.array([[1,2,3], [4,5,6]])
a_reshape = a.reshape(-1)
a_list = a_reshape.tolist()
print(a_list)

# list()方法
a = np.array([1,2,3])
a_list = list(a)
print(a_list)

Output result:

[[1, 2, 3], [4, 5, 6]]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3]

The above is the detailed content of Convert numpy to list: Tips to improve data processing efficiency. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn