问题:Calculate the mean and standard deviation of a tightly clustered set of 1000 initial conditions as a function of iteration number. The bunch of initial conditions should be Gaussian distributed about x = 0.3 with a standard deviation of 10-3
我写的代码:
from numpy import *
def IterateMap(x,r,n):
for i in xrange(n):
x = r * x * (1.0 - x)
return x
output = "data"
nIterations = 1000
r = 4.0
x0 = 0.3
delta = 0.00005
L = []
for i in xrange(nIterations):
x = x0
x = IterateMap(x,r,1)
L[i] = x
x0 = x0 + delta
A = array(L)
print 'mean: ', mean(A)所以我的代码应该做的是获取x的初始值( x0 ),然后调用IterateMap函数,返回一个新的x值,并将其放入列表(L)中,然后x0更改为一个新值,这个过程持续1000次。我得到错误“列表分配索引超出范围”。另外,你认为我是否正确地理解了这个问题?
发布于 2012-12-04 15:25:47
当您寻址超过当前大小的索引时,Python列表不会自动增长。您创建了一个空列表,因此不能对其中的任何索引进行寻址。
使用.append()将新值添加到列表的末尾:
L.append(x)或者,您必须创建一个列表,该列表可以包含您想要生成的所有索引,方法是预先填充None或其他默认值:
L = [None for _ in xrange(nIterations)]发布于 2012-12-04 15:28:05
问题出在这里:
L = [] # the list is empty!
for i in xrange(nIterations):
...
L[i] = x要修复此问题,请使用L.append(x)替换L[i] = x。
https://stackoverflow.com/questions/13697936
复制相似问题