IndexError:列表索引超出范围,用于创建变量结构库

时间:2018-05-23 09:13:56

标签: python python-3.x list numpy scipy

您好我正在尝试使用不同的组合构建模型结构库。结构库基于5个变量,我需要五个模型。 i-e modelsOne,modelsTwo等最多五个。为简单起见,我将代码粘贴到所有五个模型中。它的变化很小。 当我执行代码时,我收到以下错误

  

modelOne = modelsOne [temp1]

     

IndexError:列表索引超出范围

def ModelsProduct(modelsOne, modelsTwo, modelsThree, modelsFour,modelsFive):


    modelsStepOne = list(product("+-",repeat = 4))
    modelsStepThree = [('a','a'),('a','b'),('a','c'),('a','d'),('a','e'),('b','b'),('b','c'),('b','d'),('b','e'),('c','c'),('c','d'),('c','e')]
    modelsStepFour = [('d','d'),('d','e')]
    modelsStepFive = [('e','e')]



    #produce modelsOne
    modelsStepTwo = [('d',),('e',)]
    for one in modelsStepOne:
        for two in modelsStepTwo:
            for three in modelsStepThree:
                for four in modelsStepFour:
                    for five in modelsStepFive:
                        modelsOne.append(one+two+three+four+five)

    #produce modelsTwo
    modelsStepTwo = [('c',),('d',)]
    for one in modelsStepOne:
        for two in modelsStepTwo:
            .
            .
            .
   return modelsOne, modelsTwo, modelsThree, modelsFour, modelsFive


modelsOne, modelsTwo,modelsThree, modelsFour,modelsFive = ModelsProduct(modelsOne, modelsTwo, modelsThree, modelsFour, modelsFive)

VarList = ["a","b","c","d","e"]
initial_condi = [10, 15, 5,4,5]

dictVar = {'a':0, 'b': 1, 'c': 2, 'd': 3, 'e':4}
ops = { "+": operator.add, "-": operator.sub }
t_range = arange(0.0,60.0,1.0)


def odeFunc(Y, t, x,dictVar):

    if x[-5] == 3072:
        temp1 = 3071
    else:
        temp1 = int(x[-5])

    if x[-4] == 3072:
        temp2 = 3071
    else:
        temp2 = int(x[-4])


    if x[-3] == 3072:
        temp3 = 3071
    else:
        temp3 = int(x[-3])

    if x[-2] == 3072:
        temp4 = 3071
    else:
        temp4 = int(x[-2])

    if x[-1] == 3072:
        temp5 = 3071
    else:
        temp5 = int(x[-1])

    modelOne = modelsOne[temp1]
    modelTwo = modelsTwo[temp2]
    modelThree = modelsThree[temp3]
    modelFour = modelsFour[temp4]
    modelFive = modelsFive[temp5]
    return GenModel(Y, x, modelOne,modelTwo,modelThree,modelFour,modelFive, dictVar)

def GenModel(Y,x,modelOne,modelTwo,modelThree,modelFour,modelFive, dictVar):
    dydt = zeros_like(Y)

    dydt[0] = ops[modelOne[0]](dydt[0],x[0]*Y[0])#  k1 x1
    dydt[0] = ops[modelOne[1]](dydt[0],x[1]*Y[dictVar[modelOne[-5]]])# k2 x2
    dydt[0] = ops[modelOne[2]](dydt[0],x[2]*Y[dictVar[modelOne[-4]]])# k3 x3
    dydt[0] = ops[modelOne[3]](dydt[0],x[3]*Y[dictVar[modelOne[-3]]])# k4 x4
    dydt[0] = ops[modelOne[4]](dydt[0],x[4]*Y[dictVar[modelOne[-2]]]*Y[dictVar[modelOne[-1]]])#k5 x5 x6
    dydt[0] = ops[modelOne[5]](dydt[0],x[5])# k6

    dydt[1] = ops[modelTwo[0]](dydt[1],x[6]*Y[0])
    dydt[1] = ops[modelTwo[1]](dydt[1],x[7]*Y[dictVar[modelTwo[-5]]])
    dydt[1] = ops[modelTwo[2]](dydt[1],x[8]*Y[dictVar[modelTwo[-4]]])
    dydt[1] = ops[modelTwo[3]](dydt[1],x[9]*Y[dictVar[modelTwo[-3]]])
    dydt[1] = ops[modelTwo[4]](dydt[1],x[10]*Y[dictVar[modelTwo[-2]]]*Y[dictVar[modelTwo[-1]]])
    dydt[1] = ops[modelTwo[5]](dydt[1],x[11])
.
.
.
.   return dydt

1 个答案:

答案 0 :(得分:1)

在您的代码中,以下列表的长度为16

list(product("+-",repeat = 4))

但是,看起来像temp1 temp1 = 3071

所以,访问时 modelOne = modelsOne[temp1] 所以,这可能会导致错误