IndexError:列表索引在使用列表时超出范围[复制]

时间:2021-01-02 16:38:44

This question already has an answer here:

这个问题已经有了答案:

I am trying to run the following script, but get the error

我正在尝试运行下面的脚本,但是得到错误。

IndexError: list index out of range

I have read that this is because when you create a list it is initially empty so you need to assign to it some value which I have done by doing the following

我已经读过了,这是因为当你创建一个列表时,它最初是空的,所以你需要给它赋值,我已经做了如下的事情。

q.append(0)

but I still get the error. Can someone point out what I an doing wrong? Thank you!

但我还是会犯错误。有人能指出我做错了什么吗?谢谢你!

import numpy
from numpy import *
import matplotlib.pyplot as plt

pfa = []                        #Create lists that will hold pf,qf values
qfa = []
pf = []
qf = []
p = []
q = []
pf.append(0)
qf.append(0)
p.append(0)
q.append(0)
q[0]  = -0.5         # initial p and q values
p[0]  = 0
h = 0.001
for i in range(10):

  k1 = -h*sin(q[i])
  j1 = h*(p[i])
  k2 = -h*sin(q[i]+(1/2)*j1)
  j2 = h*p[i]*(q[i]+(1/2)*k1)             
  k3 = -h*sin(q[i]+(1/2)*j2)
  j3 = h*p[i]*(q[i]+(1/2)*k2)
  k4 = -h*sin(q[i]+(1/2)*j3)
  j4 = h*p[i]*(q[i]+(1/2)*k3)
  pf[i+1] = p[i] +(h/6.0)*(k1+2*k2+2*k3+k4)
  qf[i+1] = q[i] +(h/6.0)*(j1+2*j2+2*j3+j4)
  pfa.append(pf)                   #append lists
  qfa.append(qf)

plt.plot(qfa,pfa)
plt.show()

the trace back and error

回溯和错误。

Traceback (most recent call last):
File "C:\Documents and Settings\My Documents\Symplectic Integrators\RK4_2.py", line  23, in <module>
j1 = h*(p[i])
IndexError: list index out of range

1 个解决方案

#1


2  

Your lists contain only one element, and you are trying to access members at positions 0 to 9. Think about it:

您的列表只包含一个元素,您正在尝试访问位置0到9的成员。想想看:

>>> p = []
>>> p.append(0)
>>> p
[0]
>>> for i in range(2):
...     print "position {0}, value {1}".format(p[i], i)
... 
position 0, value 0
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list index out of range

When you do p[i], p must have at least i + 1 size.

当你做p[i]时,p必须至少有i + 1大小。

And take a look about at your pf[i] == p[i], it is an expression, not an assignment.

看看你的pf[i] == p[i],它是一个表达式,而不是一个赋值。

Maybe what you need is something like:

也许你需要的是:

pf = []
for i in range(10):
    ...
    # at this point pf.append() assigns to position i

    pf.append(p[i] + (h / 6.0) * (k1 + 2*k2 + 2*k3 + k4))
    ...

But be careful, because p needs to contain all the values you need before the for loop.

但是要小心,因为p需要包含for循环之前所需的所有值。

#1


2  

Your lists contain only one element, and you are trying to access members at positions 0 to 9. Think about it:

您的列表只包含一个元素,您正在尝试访问位置0到9的成员。想想看:

>>> p = []
>>> p.append(0)
>>> p
[0]
>>> for i in range(2):
...     print "position {0}, value {1}".format(p[i], i)
... 
position 0, value 0
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list index out of range

When you do p[i], p must have at least i + 1 size.

当你做p[i]时,p必须至少有i + 1大小。

And take a look about at your pf[i] == p[i], it is an expression, not an assignment.

看看你的pf[i] == p[i],它是一个表达式,而不是一个赋值。

Maybe what you need is something like:

也许你需要的是:

pf = []
for i in range(10):
    ...
    # at this point pf.append() assigns to position i

    pf.append(p[i] + (h / 6.0) * (k1 + 2*k2 + 2*k3 + k4))
    ...

But be careful, because p needs to contain all the values you need before the for loop.

但是要小心,因为p需要包含for循环之前所需的所有值。