Home>Article>Backend Development> The difference between python arrays and lists

The difference between python arrays and lists

(*-*)浩
(*-*)浩 Original
2019-06-25 15:45:47 28939browse

The list in python is a built-in data type of python. The data types in the list do not have to be the same, but the types in the array must all be the same. The data type in the list saves the address where the data is stored. Simply put, it is a pointer, not data. It is too troublesome to save a list. For example, list1=[1,2,3,'a'] requires 4 Pointers and four data, increase storage and consume CPU. The array encapsulated in numpy has very powerful functions, and the same data types are stored in it

The difference between python arrays and lists

Python itself does not have an array type, but it There are array types in the Numpy library.Recommended learning:Python video tutorial)

Both can be used to process multi-dimensional arrays.

The ndarray object in Numpy is used to process multi-dimensional arrays, and it serves as a fast and flexible big data container. Python lists can store one-dimensional arrays, and multi-dimensional arrays can be realized by nesting lists.

2 Storage efficiency and input and output performance are different.

Numpy is specially designed for the operation and calculation of arrays. The storage efficiency and input and output performance are far better than nested lists in Python. The larger the array, the more obvious the advantages of Numpy are.

3Element data type.

Generally, the type of all elements in a Numpy array must be the same, while the type of elements in a Python list is arbitrary, so Numpy arrays are not as good as Python lists in terms of general performance, but in scientific computing, they can It saves a lot of loop statements, and the code usage is much simpler than Python lists.

Creation of array

When creating a Numpy array, the parameter can be either a list or a tuple. For example:

>>> a=np.array((1,2,3))#参数是tuple >>> b=np.array([6,7,8])#参数是list >>> c=np.array([[1,2,3],[4,5,6]])#参数是二维list

In addition, you can also use other methods provided by numpy to create an array, for example:

>>> arr1=np.arange(1,10,1) >>> arr2=np.linspace(1,10,10)

np.arange(a,b,c) means to generate an array from a-b An array including b with interval c. The default data type is int32. But linspace(a,b,c) means dividing a-b equally into c points, which includes b.

For more Python related technical articles, please visit thePython Tutorialcolumn to learn!

The above is the detailed content of The difference between python arrays and lists. 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