python對時間序列處理時資料小數位數混亂
PHP中文网
PHP中文网 2017-05-18 10:58:09
0
1
661

想對時間序列進行狀態劃分,時間序列最大值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)
某草草

保留兩位小數就行了

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板