Python双循环?虽然和循环?

时间:2012-12-01 17:29:50

标签: python loops for-loop while-loop double

我想循环一系列c值?我怎么做?我尝试在外面使用for循环或while循环:

注意:我希望c在4到15之间,有120个值

for c in range(4,15):
    i=0
    while i< nt-1: #if i<nt-1 is true, the body below will be excuted
        #Update the acceleration using the following equation
        x[i+1] = (-y[i]-z[i])*deltat+x[i]
        #Update the velocity using the follwing equation
        y[i+1] =(x[i]+a*y[i])*deltat+y[i]
        #Update the displacement, using follwing equation
        z[i+1] = (b+z[i]*(x[i]-c))*deltat+z[i]
        #go to next time step
        i=i+1

OR

c=arange(4,15,1)
for p in c:
   while i< nt-1: #if i<nt-1 is true, the body below will be excuted
        #Update the acceleration using the following equation
        x[i+1] = (-y[i]-z[i])*deltat+x[i]
        #Update the velocity using the follwing equation
        y[i+1] =(x[i]+a*y[i])*deltat+y[i]
        #Update the displacement, using follwing equation
        z[i+1] = (b+z[i]*(x[i]-p))*deltat+z[i]
        #go to next time step
        i=i+1

1 个答案:

答案 0 :(得分:2)

由于您已经在使用NumPy,以下可能是最自然的:

for c in np.linspace(4, 15, 120):
   ...

linspace()调用产生120个值:

In [33]: np.linspace(4, 15, 120)
Out[33]: 
array([  4.        ,   4.09243697,   4.18487395,   4.27731092,
         4.3697479 ,   4.46218487,   4.55462185,   4.64705882,
         4.7394958 ,   4.83193277,   4.92436975,   5.01680672,
        ...
        14.35294118,  14.44537815,  14.53781513,  14.6302521 ,
        14.72268908,  14.81512605,  14.90756303,  15.        ])