# Triple nested loop python (While loop insde of for loop inside ofwhile loop)

Discussion in 'Python' started by Isaac Won, Mar 1, 2013.

1. ### Isaac WonGuest

try to make my triple nested loop working. My code would be:
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./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.*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

Isaac Won, Mar 1, 2013

2. ### Ulrich EckhardtGuest

Am 01.03.2013 09:59, schrieb Isaac Won:
This is bad style and you shouldn't do that in python. The question that
comes up for me is whether something else is modifying "c" in that loop,
but I think the answer is "no". For that reason, use Python's way:

for c in range(5, 25):
...

That way it is also clear that the first value in the loop is 5, while
the initial "c = 4" seems to suggest something different. Also, the last
value is 24, not 23.

Apart from the wrong indention (don't mix tabs and spaces, see PEP 8!)
and the that "d in range(336)" is better style, you don't start with an
empty "y1", except on the first iteration of the outer loop.

drop everything not necessary to demostrate the problem before posting.
This makes it easier to see what is going wrong both for you and others.
Also make sure that others can actually run the code.

Greetings from Hamburg!

Uli

Ulrich Eckhardt, Mar 1, 2013

3. ### Joel GoldstickGuest

On Fri, Mar 1, 2013 at 7:00 AM, Ulrich Eckhardt <
[y]? Is this really a list? So what is y1 + y1 + [y] doing?
dt = 900.0 #Time step (seconds)
fs = 1./dt #Sampling frequency

This should me moved out of the loop since nothing changes with dt or fs

Joel Goldstick, Mar 1, 2013
4. ### Chris AngelicoGuest

Note your indentation levels: the code does not agree with your
subject line. The third loop is not actually inside your second.
Should it be?

ChrisA

Chris Angelico, Mar 1, 2013
5. ### Isaac WonGuest

Thank you, Chris.
I just want to acculate value from y repeatedly.
If y = 1,2,3...10, just have a [1,2,3...10] at onece.

Isaac Won, Mar 1, 2013
6. ### Isaac WonGuest

Thank you, Chris.
I just want to acculate value from y repeatedly.
If y = 1,2,3...10, just have a [1,2,3...10] at onece.

Isaac Won, Mar 1, 2013
7. ### Isaac WonGuest

Yes, the thiird lood should be inside of my whole loop.
Thank you,
Isaac

Isaac Won, Mar 1, 2013
8. ### Isaac WonGuest

Yes, the thiird lood should be inside of my whole loop.
Thank you,
Isaac

Isaac Won, Mar 1, 2013
9. ### Isaac WonGuest

What I really want to get from this code is m1 as I told. For this purpose, for instance, values of fpsd upto second loop and that from third loop should be same, but they are not. Actually it is my main question.
Thank you,
Isaac

Isaac Won, Mar 1, 2013
10. ### Ulrich EckhardtGuest

Am 01.03.2013 17:28, schrieb Isaac Won:
You are not helping yourself...

Read this carefully, I didn't write that to fill up empty space. Also,
read Eric S. Raymond's essay on asking smart questions (you can easily
locate it online), which the problems with your question in a much more
general way.

Uli

Ulrich Eckhardt, Mar 4, 2013