Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use arrays and lists in python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use arrays and lists in python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

# Environment win64+anaconda+python3.6

List & array

(1) list does not have all the attributes of array (such as dimensions, transpositions, etc.)

Code 1:

# eg1_1import numpy as npa = np.array ([[1meme 2je 0je 1], [1pje 6je 9je 55], [7m 8je 9je 5]]) # an is an array print (a.T) # Result: [[1 17] [2 68] [0 9] [1 555]] # eg1_2a = [1m 2], [1m 6, 9, 55], [7, 8, 9, 5]] # an is a list print (a.T) # Result:'list' object has no attribute'T'

Code 2:

# eg1_3import numpy as npa=np.array ([[1je 2rect 3], [1jre 1je 4], [1je 5je 1]]) print (a.shape) # Result: (3,3) # eg1_4a= [[1je 2jue 3], [1je 1je 4], [1je 5je 1]] print (a.shape) # Result'list' object has no attribute 'shape'

(by the way, how to convert an array to a column vector: ↓)

Import numpy as npa=np.array ([[1Jing 2jue 3], [1Jer 1Jer 4], [1Jing 5je 1]]) a=a.reshape (- 1je 1) print (a) # Result: [[1] [2] [3] [1] [4] [1] [5] [1]]

(2) the meaning of a [: M], a can be a list or an array, but in either case, a [: 0] is empty

# eg2_1import numpy as npa=np.array ([[4meme 1rect 2], [7jre 4je 10], [12je 17pr 88]) # a=np.array ([(4je 1pje 2), # (7je 4pr 10), # (12pr 17pr 88)])) in these two a [and (different) In fact, they are exactly the same. Print (a [: 0]) print (a [: 1]) print (a [: 2]) # Result: [] [[412]] [4 12] [7 410]] # eg2_1a= [(4, 4, 4, 10), (12, 17, 88)] print (a [: 0]) print (a [: 1]) print (a [: 2]) # Result: [] [(4, 1, 2)] [(4, 1, 2)] [(4, 1, 2)] [(4, 1) 2), (7) 4, 10)]

(3) the calculation of "=" by array and list

# eg3_1import numpy as npa=np.array (['dog','cat','car']) b=np.array ([' dog','cat','trunk']) acc = (np.mean (a = = b) print (acc) # Result0.6666666666666666 # eg3_2import numpy as npa= ['dog','cat','car'] b = [' dog','cat','trunk'] acc = (np.mean (a = = b)) print (acc) # Result0.0

(4) calculation of "*" by array and list

From numpy import * # an is an array of a=array ([[1pje 2je 3], [4je 5je 6]]) b=4*aprint (b) [[48 12] [16 20 24]] from numpy import * # an is a list a = ([1m 2m 3], [4m 5e 6]) b=4*aprint (b) [1m 2m 3], [4m 5e 6], [1m 2m 3], [4J 5pm 6], [1J 2pm 3] [4, 5, 6], [1, 2, 3], [4, 5, 6]] differences between python list and Numpy array

1. Both can be used to deal with multi-dimensional arrays.

The ndarray object in Numpy is used to deal with multidimensional arrays as a fast and flexible big data container. Python lists can store one-dimensional arrays, and multi-dimensional arrays can be realized through the nesting of lists.

2. Storage efficiency and input and output performance are different.

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

3. Element data type.

In general, all elements in the Numpy array must be of the same type, while the element type in the Python list is arbitrary, so the Numpy array is not as good as the Python list in terms of general performance, but in scientific calculation, a lot of circular statements can be saved, and the code is much simpler than the Python list.

The above is all the content of the article "how to use arrays and lists in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report