三重嵌套循环(while循环内for循环内while循环)



我正在尝试使我的三重嵌套循环工作。

我的代码是:

c = 4
y1 = []
m1 = []
std1 = []
while c < 24:
        c = c + 1
        a = []
        f.seek(0, 0)    
        for columns in ( raw.strip().split() for raw in f ):
                a.append(columns[c])    
                x = np.array(a, float)    
        not_nan = np.logical_not(np.isnan(x))    
        indices = np.arange(len(x))    
        interp = interp1d(indices[not_nan], x[not_nan], kind = 'nearest')    
        p = interp(indices)
        N = len(p)
        dt = 900.0 #Time step (seconds)
        fs = 1.0 / dt #Sampling frequency
        KA,PSD = oned_Fourierspectrum(p,dt) # Call Song's 1D FS function
        time_axis = np.linspace(0.0, N, num = N, endpoint = False) * 15 / (60 * 24)
        plot_freq = 24 * 3600.0 * KA #Convert to cycles per day
        plot_period = 1.0 / plot_freq # convert to days/cycle
        fpsd = plot_freq * PSD
        d = -1
        while d < 335:
                d = d + 1
                y = fpsd[d]
                y1 = y1 + [y]
                m = np.mean(y1)
        m1 = m1 + [m]
print m1

我的目的是列出[mean(fpsd[0]), mean(fpsd[1]), mean(fpsd[2]).. mean(fpsd[335])]的列表。每个y1将是fpsd[d]的列表。

我检查;它在第二次while循环之前工作得很好,我可以得到fpsd[d]的个别平均值。然而,对于第二个完整的循环,它产生的数字肯定是错误的。你能帮我解决这个问题吗?

如果您希望m1是means的累积列表,那么最后一行需要缩进到最后一个while块中。

while d <335:     
    d = d + 1     
    y = fpsd[d]     
    y1 = y1 + [y]       
    m = np.mean(y1)
    m1 = m1 + [m]

相关内容

  • 没有找到相关文章

最新更新