Steven
Steven

Reputation: 321

Python list assignment index out of range

I keep getting an IndexError: list assignment index out of range. Here is my code:

import numpy as np
import asciidata

fv = [] 
fb = []
data = asciidata.open('Flux.txt')
for i in data[1]:
    fv.append(float(i))
for i in data[2]:
    fb.append(float(i))

mv = []
mb = []
for i in range (0,25):
    mv[i] = 10.1 - 2.5 * np.log(fv[i]/1220000)
    mb[i] = 11.0 - 2.5 * np.log(fb[i]/339368)
    print i, mv[i], mb[i]

Upvotes: 0

Views: 2242

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114098

mv.append(10.1 - 2.5 * np.log(fv[i]/1220000))
mb.append(11.0 - 2.5 * np.log(fb[i]/339368))

mv[i] wont work because there is no ith index

really I would use a list comprehension

mv = [10.1 - 2.5 * np.log(val/1220000) for val in fv]

and since you are using numpy you can do even better

fv = np.array(fv)
mv = 10 - np.log(fv/1220000)*2.5 

Upvotes: 6

Related Questions