python some small knowledge of basic grammar

1. Use the list as an index

  

1 a = np.around(10*np.random.random((3, 3)))
2 b = [0, 1, 2]
3 c = [0, 1, 2]
4 print(a)
5 print(a[b, c])

Results are as follows, using [0, 1, 2] as a row index, [0,1, 2] as a column, the output of a [0, 0], a [1, 1], a [2, 3].

 

2. Copy

  (. 1) a  = b b is copied to the id of b, and a and b are the same object

1 import numpy as np
2 
3 a = np.arange(5)
4 print(a)
5 b = a
6 print(id(a))
7 print(id(b))

  The results can be seen by running the diagram, a is equal to b of id id, with a pointed object.

  (2) b = a.view () is a shallow copy, b and a are different objects, but their elements are shared.

1 import numpy as np
2 
3 a = np.arange(5)
4 print(a)
5 b = a.view()
6 print(id(a))
7 print(id(b))
8 b[0] = -1
9 print(a)

  The results can be seen by running the following figure, a and b are different objects, but when I change a time element, b element also changed. '

  (3) b = a.copy () is a deep copy, b of the element is a copy of the

 

Guess you like

Origin www.cnblogs.com/loubin/p/11239466.html