【发布时间】:2015-01-05 07:38:57
【问题描述】:
我正在尝试根据获取数据的日期(或纪元)将数据文件分成列表。我试图通过告诉程序如果一个点的纪元与前一点相同,则将其添加到列表中,如果不是则继续。我目前收到错误:
第 31 行,
if epoch[i] == epoch[i+1]:
TypeError: list indices must be integers, not float
这就是我目前所拥有的(我还没有写到告诉它进入下一个时代)。
epoch=[]
wavelength=[]
flux=[]
text_file = open("datafile.dat", "r")
lines1 = text_file.read()
#print lines1
text_file.close()
a = [float(x) for x in lines1.split()]
a1=0
a2=1
a3=2
while a1<len(a):
epoch.append(float(a[a1]))
wavelength.append(float(a[a2]))
flux.append(float(a[a3]))
a1+=3
a2+=3
a3+=3
#print epoch
x=[]
y=[]
z=[]
i = epoch[0]
if epoch[i] == epoch[i+1]:
x.append(epoch[i])
y.append(wavelength[i])
z.append(flux[i])
i+=1
#print x
#print z
我不知道我需要改变什么!提前致谢。
【问题讨论】:
-
尝试将 i 转换为 int > if epoch[int(i)] == epoch[int(i)+1]:
标签: python floating-point int typeerror