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 -------------------------------------------------------------------------------- My purpose is 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 check 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?
Am 01.03.2013 09:59, schrieb Isaac Won: > try to make my triple nested loop working. My code would be: > c = 4 [...] > while c <24: > c = c + 1 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. > while d <335: > d = d + 1 > y = fpsd[d] > y1 = y1 + [y] > m = np.mean(y1) > m1 = m1 + [m] 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. I'm not really sure if that answers your problem. In any case, please 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
--bcaec54d4cfcd4687c04d6dc95c2 Content-Type: text/plain; charset=UTF-8 On Fri, Mar 1, 2013 at 7:00 AM, Ulrich Eckhardt < ulrich.eckhardt@dominolaser.com> wrote: > Am 01.03.2013 09:59, schrieb Isaac Won: > > try to make my triple nested loop working. My code would be: >> c = 4 >> > [...] > > while c <24: >> c = c + 1 >> > > 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. > > > > I concur with Uli, and add the following thoughts: What is going on with [y]? Is this really a list? So what is y1 + y1 + [y] doing? > > while d <335: >> d = d + 1 >> y = fpsd[d] >> y1 = y1 + [y] >> m = np.mean(y1) >> m1 = m1 + [m] >> > > In your outer loop you initialize these values each pass: 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 > 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. > > I'm not really sure if that answers your problem. In any case, please 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 > > > > -- > http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list> > -- Joel Goldstick http://joelgoldstick.com --bcaec54d4cfcd4687c04d6dc95c2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><br><div class=3D"gmail= _quote">On Fri, Mar 1, 2013 at 7:00 AM, Ulrich Eckhardt <span dir=3D"ltr">&= lt;<a href=3D"mailto:ulrich.eckhardt@dominolaser.com" target=3D"_blank">ulr= ich.eckhardt@dominolaser.com</a>></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex">Am 01.03.2013 09:59, schr= ieb Isaac Won:<div class=3D"im"><br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex"> try to make my triple nested loop working. My code would be:<br> c =3D 4<br> </blockquote></div> [...]<div class=3D"im"><br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex"> while c <24:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0c =3D c + 1<br> </blockquote> <br></div> This is bad style and you shouldn't do that in python. The question tha= t comes up for me is whether something else is modifying "c" in t= hat loop, but I think the answer is "no". For that reason, use Py= thon's way:<br> <br> =C2=A0 for c in range(5, 25):<br> =C2=A0 =C2=A0 =C2=A0 ...<br> <br> That way it is also clear that the first value in the loop is 5, while the = initial "c =3D 4" seems to suggest something different. Also, the= last value is 24, not 23.<div class=3D"im"><br> <br> <br></div></blockquote><div>I concur with Uli, and add the following though= ts:=C2=A0 What is going on with [y]?=C2=A0 Is this really a list?=C2=A0 So = what is y1 + y1 + [y] doing? <br></div><blockquote class=3D"gmail_quote" st= yle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padd= ing-left:1ex"> <div class=3D"im"> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex"> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while d <335:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0d =3D d + 1<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0y =3D fpsd[d]= <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0y1 =3D y1 + [= y]<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 m =3D np.mean(y1)<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0m1 =3D m1 + [m]<br> </blockquote> <br></div></blockquote><div>In your outer loop you initialize these values = each pass:<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 dt =3D 900.0 #Time step (seco= nds)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 fs =3D 1./dt #Sampling frequency<br> <br></div><div>This should me moved out of the loop since nothing changes w= ith dt or fs<br></div><div><br>=C2=A0</div><blockquote class=3D"gmail_quote= " style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);= padding-left:1ex"> <div class=3D"im"></div> 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 sta= rt with an empty "y1", except on the first iteration of the outer= loop.<br> <br> I'm not really sure if that answers your problem. In any case, please d= rop 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 m= ake sure that others can actually run the code.<br> <br> <br> Greetings from Hamburg!<br> <br> Uli<span class=3D""><font color=3D"#888888"><br> <br> <br> <br> -- <br> <a href=3D"http://mail.python.org/mailman/listinfo/python-list" target=3D"_= blank">http://mail.python.org/<u></u>mailman/listinfo/python-list</a><br> </font></span></blockquote></div><br><br clear=3D"all"><br>-- <br><div dir= =3D"ltr"><div>Joel Goldstick<br></div><a href=3D"http://joelgoldstick.com" = target=3D"_blank">http://joelgoldstick.com</a><br></div> </div></div> --bcaec54d4cfcd4687c04d6dc95c2--
On Fri, Mar 1, 2013 at 7:59 PM, Isaac Won <winefrog@gmail.com> wrote: > while c <24: > for columns in ( raw.strip().split() for raw in f ): > while d <335: 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
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. On Friday, March 1, 2013 7:41:05 AM UTC-6, Chris Angelico wrote: > On Fri, Mar 1, 2013 at 7:59 PM, Isaac Won <winefrog@gmail.com> wrote: > > > while c <24: > > > for columns in ( raw.strip().split() for raw in f ): > > > while d <335: > > > > 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
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. On Friday, March 1, 2013 7:41:05 AM UTC-6, Chris Angelico wrote: > On Fri, Mar 1, 2013 at 7:59 PM, Isaac Won <winefrog@gmail.com> wrote: > > > while c <24: > > > for columns in ( raw.strip().split() for raw in f ): > > > while d <335: > > > > 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
On Friday, March 1, 2013 7:41:05 AM UTC-6, Chris Angelico wrote: > On Fri, Mar 1, 2013 at 7:59 PM, Isaac Won <winefrog@gmail.com> wrote: > > > while c <24: > > > for columns in ( raw.strip().split() for raw in f ): > > > while d <335: > > > > 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 Yes, the thiird lood should be inside of my whole loop. Thank you, Isaac
On Friday, March 1, 2013 7:41:05 AM UTC-6, Chris Angelico wrote: > On Fri, Mar 1, 2013 at 7:59 PM, Isaac Won <winefrog@gmail.com> wrote: > > > while c <24: > > > for columns in ( raw.strip().split() for raw in f ): > > > while d <335: > > > > 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 Yes, the thiird lood should be inside of my whole loop. Thank you, Isaac
Thank you Ulich for reply, 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 On Friday, March 1, 2013 6:00:42 AM UTC-6, Ulrich Eckhardt wrote: > Am 01.03.2013 09:59, schrieb Isaac Won: > > > try to make my triple nested loop working. My code would be: > > > c = 4 > > [...] > > > while c <24: > > > c = c + 1 > > > > 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. > > > > > > > > > while d <335: > > > d = d + 1 > > > y = fpsd[d] > > > y1 = y1 + [y] > > > m = np.mean(y1) > > > m1 = m1 + [m] > > > > 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. > > > > I'm not really sure if that answers your problem. In any case, please > > 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
Am 01.03.2013 17:28, schrieb Isaac Won: > 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. You are not helping yourself... >> In any case, please 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. 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