Python中的整型/浮点型错误

Python中的整型/浮点型错误,python,floating-point,int,typeerror,Python,Floating Point,Int,Typeerror,我正试图根据数据的获取日期(或时代)将数据文件分为列表。我试图通过告诉程序,如果一个点的历元与前一个点相同,则将其添加到列表中,如果不是,则继续。我当前收到错误信息: 第31行 if epoch[i] == epoch[i+1]: TypeError: list indices must be integers, not float 这就是我目前所拥有的(我还没有写下一点告诉它进入下一个时代) epoch=[] 波长=[] 通量=[] text_file=open(“datafile.

我正试图根据数据的获取日期(或时代)将数据文件分为列表。我试图通过告诉程序,如果一个点的历元与前一个点相同,则将其添加到列表中,如果不是,则继续。我当前收到错误信息:

第31行

    if epoch[i] == epoch[i+1]:
TypeError: list indices must be integers, not float
这就是我目前所拥有的(我还没有写下一点告诉它进入下一个时代)

epoch=[]
波长=[]
通量=[]
text_file=open(“datafile.dat”、“r”)
lines1=text_file.read()
#打印行1
text_file.close()
a=[第1.split()行中x的浮点(x)]
a1=0
a2=1
a3=2

当a1使用这一行在列表中放置一个浮点时,Python无法使用这些作为索引,因为它们不是确定的值:

epoch.append(float(a[a1]))
这个错误告诉你你需要知道的一切。只需将
i
转换为
int

i = int(epoch[0])

此行将
历元中的值存储为浮点数:

epoch.append(float(a[a1]))
然后尝试使用
epoch
的第一个值访问
epoch

i = epoch[0]
if epoch[i] == epoch[i+1]:
错误是告诉您不能使用
浮动
作为索引来访问列表。因此,您需要在
epoch
中将值存储为
int
,或者在将其用作索引之前转换为
int

在这一行中:

epoch.append(float(a[a1]))
在附加到列表之前,您正在将所有项强制转换为浮动

因此,索引i的初始化:

i = epoch[0]
将始终包含不允许作为索引的浮动(2.5作为索引没有意义)

您需要做的只是将索引i转换为整数:

i = int(epoch[0])
替换:

i = epoch[0]
作者:


如果epoch[int(i)]==epoch[int(i)+1]:
i = epoch[0]
i = 0