编程就是踩坑的过程。今天又踩了一个坑,做个积累吧。
在给数组赋初始值的时候,经常会用到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类型,要使用别的类型需要加符号的,我就是在这个上面躺枪了。
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |