Fork me on GitHub

  编程就是踩坑的过程。今天又踩了一个坑,做个积累吧。

在给数组赋初始值的时候,经常会用到0数组,而Python中,我们使用zero()函数来实现。在默认的情况下,zeros创建的数组元素类型是浮点型的,如果要使用其他类型,可以设置dtype参数进行声明。下面通过一个例子来说明:

  1默认参数的例子:

  

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
~                  

  运行结果:

[[  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]]

 

  2 参数为整形的例子:

 1 import numpy as np
 2 
 3 r = 10
 4 c = 10
 5 
 6 #D0 = np.zeros((11,11))
 7 D0 = np.zeros((11,11),dtype=int)
 8 
 9 D0[0, 1:] = 32767
10 D0[1:, 0] = 32767
11 
12 for i in range(r):
13         for j in range(c):
14             D0[i + 1, j + 1] = 1 
15 print D0

  运行结果:

[[    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]]

  可以看出,该zeros默认的是float类型,要使用别的类型需要加符号的,我就是在这个上面躺枪了。

posted on 2018-11-28 10:57  虚生  阅读(14269)  评论(0编辑  收藏  举报