我的代码给了我以下错误:
list_subs[n][4] = np.random.normal(list_subs[n][1], list_subs[n][2])
IndexError: list assignment index out of range.
我已经搜索了这个错误,但我仍然找不到问题所在。
编辑:完整回溯
回溯(最后一次调用):文件“”,第 420 行,run_nodebug 文件“C:\Documents and Settings\jhsilva\Desktop\Monte carlo\Teste.py”,第 71 行,generateRandomNumbers(list_subs) 文件“C: \Documents and Settings\jhsilva\Desktop\Monte carlo\Teste.py",第 41 行,在 generateRandomNumbers list_subs[n][4] = np.random.normal(list_subs[n][1], list_subs[n][2 ]) IndexError:列表分配索引超出范围
编码
def generateRandomNumbers(list_subs):
for n in range(len(list_subs)):
string = list_subs[n][3]
string = string.lower()
if(string == "normal"):
list_subs[n][4] = np.random.normal(list_subs[n][1], list_subs[n][2])
print("Numero gerado:",list_subs[n][4])
variables = [v for v in variables if v not in special]
list_subs=[[0 for col in range(6)] for row in range(len(variables)-1)]
#This prints fine
print(len(list_subs))
#this prints fine too
print(list_subs[0][4])
for n in range(len(variables)):
if n>0:
(media,desviopadrao,distribuicao) = eval(input("For variable "+variables[n]+" input: median, std, distr \n"))
list_subs[n-1] = [variables[n], media, desviopadrao, distribuicao]
N = eval(input("Qual o numero de iteracoes que deseja:"))
Var = []
for n in range(N):
generateRandomNumbers(list_subs)
Var.append(calcEq(formula))