0

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]
3
  • 2
    What is the full traceback? Commented Jun 4, 2013 at 19:18
  • Traceback (most recent call last): File "C:\Python27\StevenScripts\Opdracht 1 PS\Flux.py", line 15, in <module> mv[i] = 10.1 - 2.5 * np.log(fv[i]/1220000) IndexError: list assignment index out of range Commented Jun 4, 2013 at 19:19
  • 1
    You can edit your post to add that kind of information. Commented Jun 4, 2013 at 19:19

1 Answer 1

6
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 
Sign up to request clarification or add additional context in comments.

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.