python对时间序列处理时数据小数位数混乱
PHP中文网
PHP中文网 2017-05-18 10:58:09
0
1
652

想对时间序列进行状态划分,时间序列最大值1.8,最小值-0.5,划分为115个状态区间,每个状态区间宽度为0.02,把处于各状态区间的值化成区间中间的值,写了一段代码,但是得到的值有的小数位数很奇怪,请大神帮忙解惑,谢谢

__author__ = 'Administrator'
file = open('yij.txt','r')

while 1:
    line = file.readline()
    a = line.split()
    m=0
    b=[-0.5,-0.48,-0.46,-0.44,-0.42,-0.40,-0.38,-0.36,-0.34,-0.32,-0.30,-0.28,-0.26,-0.24,-0.22,-0.20,-0.18,-0.16,-0.14,-0.12,-0.10,-0.08,-0.06,-0.04,-0.02,0.00,0.02,
   0.04,0.06,0.08,0.10,0.12,0.14,0.16,0.18,0.20,0.22,0.24,0.26,0.28,0.30,0.32,0.34,0.36,0.38,0.40,0.42,0.44,0.46,0.48,0.50,0.52,0.54,0.56,0.58,0.60,0.62,0.64,
   0.66,0.68,0.70,0.72,0.74,0.76,0.78,0.80,0.82,0.84,0.86,0.88,0.90,0.92,0.94,0.96,0.98,1.00,1.02,1.04,1.06,1.08,1.10,1.12,1.14,1.16,1.18,1.20,1.22,1.24,1.26,1.28,
   1.30,1.32,1.34,1.36,1.38,1.40,1.42,1.44,1.46,1.48,1.50,1.52,1.54,1.56,1.58,1.60,1.62,1.64,1.66,1.68,1.70,1.72,1.74,1.76,1.78,1.80]
    if line != '':
        file1 = open('yij_state.txt', 'a+')
        for i in b:
            if i<float(a[0])<=i+0.02:
                file1.write("%.2f\n" % (i+0.01))
            if float(a[0])<=-0.5:
                file1.write("%.2f\n" % (-0.49))
            elif float(a[0])>1.8:
                file1.write("%.2f\n" % (1.79))
                break
        file1.close
    else:
        break
file.close

代码已经改正确了

PHP中文网
PHP中文网

认证0级讲师

全部回复(1)
某草草

保留两位小数就行了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板