0

I am trying to make my triple nested loop work.

My code is like:

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

My purpose is to make a list of [mean(fpsd[0]), mean(fpsd[1]), mean(fpsd[2]).. mean(fpsd[335])]. Each y1 would be the list of fpsd[d].

I checked; it is working pretty well before second while loop, and I can get individual mean of fpsd[d]. However, with second whole loop, it produces definitely wrong numbers. Would you help me this problem?

3
  • There is no while loop inside a for loop. They are independent of each other - unless you mixed up indentation. Commented Mar 1, 2013 at 9:45
  • @ glgl Yes, you are right. I want to put while loop inside of for loop. But it caused indentation error. As you said, I mixed up indentation. I have not much idea to figure it out. If you have any idea, I will really appreciate. Commented Mar 1, 2013 at 9:58
  • Well, I don't know what you are planning to do. Indent your stuff the way you want it to work (probably the lines below the for loop must be indented to the level of the for loop) and you are one step closer towars your goal. Commented Mar 1, 2013 at 10:15

1 Answer 1

1

If you want m1 to be the cumulative list of means, then the last line needs to be indented to be inside the final while block.

while d <335:     
    d = d + 1     
    y = fpsd[d]     
    y1 = y1 + [y]       
    m = np.mean(y1)
    m1 = m1 + [m]
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for reply Radio, I did with your advice, but it didn;t work and didn't produce the values which I expect. It seems due to relationship between for loop and second whle loop, rather than inside second while loop.
@Isaac Where are they both related?
@ Glgl If you ask about for loop and while loop. For loop is used to extract columns from txt file and spectrum densities were calculated for each column and I just want to calculate means and standard deviations of spectra at each frequency point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.