The number of decimal places in the data is confusing when Python processes time series
PHP中文网
PHP中文网 2017-05-18 10:58:09
0
1
469

I want to divide the time series into states. The maximum value of the time series is 1.8 and the minimum value is -0.5. It is divided into 115 state intervals. The width of each state interval is 0.02. The value in each state interval is converted into the value in the middle of the interval. I wrote a piece of code, but the number of decimal places in the value obtained is very strange. Please help me solve the problem. Thank you

__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

The code has been corrected

PHP中文网
PHP中文网

认证0级讲师

reply all(1)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!