循环功能仅返回最后一个值

我想对具有3个返回值(每个值是一个列表)的函数执行for循环-所以我应该获得3个列表。我确实获得3个列表,但仅包含i的最后一个值。

def HHmodel(I,length, area):

        v = []
        m = []
        h = []
        n = []
        a= []
        dt = 0.05
        t = np.linspace(0,100,length)


        #constants
        Cm = 1.0*(1+0.1*j)#microFarad
        ENa=50 #miliVolt
        EK=-77  #miliVolt
        El=-54 #miliVolt
        g_Na=120 *(1+0.1*j)#mScm-2
        g_K=36 *(1+0.1*j)#mScm-2
        g_l=0.03 *(1+0.1*j)#mScm-2

        def alphaN(v):
            return 0.01*(v+50)/(1-np.exp(-(v+50)/10))

        def betaN(v):
            return 0.125*np.exp(-(v+60)/80)

        def alphaM(v):
            return 0.1*(v+35)/(1-np.exp(-(v+35)/10))

        def betaM(v):
            return 4.0*np.exp(-0.0556*(v+60))

        def alphaH(v):
            return 0.07*np.exp(-0.05*(v+60))

        def betaH(v):
            return 1/(1+np.exp(-(0.1)*(v+30)))

        #Initialize the voltage and the channels :
        v.append(-60)
        m0 = alphaM(v[0])/(alphaM(v[0])+betaM(v[0]))
        n0 = alphaN(v[0])/(alphaN(v[0])+betaN(v[0]))
        h0 = alphaH(v[0])/(alphaH(v[0])+betaH(v[0]))

        #t.append(0)
        m.append(m0)
        n.append(n0)
        h.append(h0)

        #solving ODE using Euler's method:
        for i in range(1,len(t)):
            m.append(m[i-1] + dt*((alphaM(v[i-1])*(1-m[i-1]))-betaM(v[i-1])*m[i-1]))
            n.append(n[i-1] + dt*((alphaN(v[i-1])*(1-n[i-1]))-betaN(v[i-1])*n[i-1]))
            h.append(h[i-1] + dt*((alphaH(v[i-1])*(1-h[i-1]))-betaH(v[i-1])*h[i-1]))
            gNa = g_Na * h[i-1]*(m[i-1])**3
            gK=g_K*n[i-1]**4
            gl=g_l
            INa = gNa*(v[i-1]-ENa)
            IK = gK*(v[i-1]-EK)
            Il=gl*(v[i-1]-El)
            v.append(v[i-1]+(dt)*((1/Cm)*(I[i-1]-(INa+IK+Il))))
            a.append(area)
            #v.append(v[i-1]+(dt)*((1/Cm)*(I-(INa+IK+Il))))
        return v,t,a

现在

for j in range(0, 10):
    barcode = np.zeros(length)
    noisyI = np.random.normal(0,9,length)
    area = 1.0 + 0.01*j
    v,t,area = HHmodel(noisyI,length,area)

它只调用最后一个j的函数,而不是...我想返回j乘以3个列表。

我知道这应该是早期绑定的事情,但这是行不通的。

BR

评论
  • 呐爱
    呐爱 回复

    You assign to the variables v,t and area every loop, so the value gets overwritten. If you would like them to be lists, you can append to a list in the for loop instead, like this:

    v, t, area =[], [], []
    
    for j in range(0, 10):
        # calculate stuff
        res = HHmodel(noisyI,length,area)
        v.append(res[0])
        t.append(res[1])
        area.append(res[2])