python zeros usage examples

Programming is stepped pit process. Today I stepped on a pit, to be accumulating it.

When the initial value assigned to the array, often used 0 array, and Python, we use zero () function to achieve. By default, the array elements are zeros to create the type of floating-point type, if you want to use a different type, you can set the parameters dtype statement. Here will be described through an example:

1 Examples of default parameters:

import numpy as np

r = 10
c = 10

D0 = np.zeros((11,11))
#D0 = np.zeros((11,11),dtype=int)

D0[0, 1:] = 32767
D0[1:, 0] = 32767

for i in range(r):
        for j in range(c):
            D0[i + 1, j + 1] = 1 
print D0
~

operation result:

[[  0.00000000e+00   3.27670000e+04   3.27670000e+04   3.27670000e+04
    3.27670000e+04   3.27670000e+04   3.27670000e+04   3.27670000e+04
    3.27670000e+04   3.27670000e+04   3.27670000e+04]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]
 [  3.27670000e+04   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00   1.00000000e+00   1.00000000e+00]]

Examples 2 to integer parameter:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import numpy as np

r = 10
c = 10

#D0 = np.zeros((11,11))
D0 = np.zeros((11,11),dtype=int)

D0[0, 1:] = 32767
D0[1:, 0] = 32767

for i in range(r):
        for j in range(c):
            D0[i + 1, j + 1] = 1 
print D0

operation result:

[[    0 32767 32767 32767 32767 32767 32767 32767 32767 32767 32767]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]
 [32767     1     1     1     1     1     1     1     1     1     1]]

As can be seen, the zeros is the default type float, to use other types require plus sign, and I was lying in the top of the gun.

Published 705 original articles · won praise 829 · Views 1.32 million +

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/105141565