0

I'm trying to seperate a data file into lists depending on what day (or epoch) the data was taken. I'm trying to do it by telling the program that if the epoch of one point is the same as the previous point, add it to a list, and if not then to move on. I'm currently getting the error:

line 31,

    if epoch[i] == epoch[i+1]:
TypeError: list indices must be integers, not float

This is what I currently have (I haven't written the bit telling it to move onto the next epoch yet).

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 can't work out what I need to change! Thanks in advance.

1
  • Try to cast i to int > if epoch[int(i)] == epoch[int(i)+1]: Commented Nov 8, 2014 at 16:38

4 Answers 4

1

You put a float in the list with this line- Python can't work with these for indices as they aren't definite values:

epoch.append(float(a[a1]))

The error tells you everything that you need to know. Just cast i to an int:

i = int(epoch[0])
Sign up to request clarification or add additional context in comments.

Comments

1

This line stores values in epoch as floats:

epoch.append(float(a[a1]))

Then you try to access epoch using the first value of epoch:

i = epoch[0]
if epoch[i] == epoch[i+1]:

The error is telling you that you cannot use a float as an index to access a list. So you either need to store the value as int in epoch, or cast to an int before using it as an index.

Comments

0

In this line:

epoch.append(float(a[a1]))

You are casting all items to floats before appending to the list epoch.

So your initialization of index i:

i = epoch[0]

Will always contain floats which aren't allowed as an index (2.5 makes no sense as an index).

What you need to do, is simply cast your index i to integer:

i = int(epoch[0])

Comments

0

Replace :

i = epoch[0]

by :

i = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.