Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40236 > unrolled thread
| Started by | Isaac Won <winefrog@gmail.com> |
|---|---|
| First post | 2013-03-01 00:59 -0800 |
| Last post | 2013-03-01 08:18 -0800 |
| Articles | 10 — 4 participants |
Back to article view | Back to comp.lang.python
Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 00:59 -0800
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-03-01 13:00 +0100
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-01 08:00 -0500
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 08:28 -0800
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-03-04 11:08 +0100
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Chris Angelico <rosuav@gmail.com> - 2013-03-02 00:41 +1100
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 08:14 -0800
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 08:14 -0800
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 08:18 -0800
Re: Triple nested loop python (While loop insde of for loop inside of while loop) Isaac Won <winefrog@gmail.com> - 2013-03-01 08:18 -0800
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 00:59 -0800 |
| Subject | Triple nested loop python (While loop insde of for loop inside of while loop) |
| Message-ID | <118003ed-33f6-4d99-ac5e-99ec81921f45@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2013-03-01 13:00 +0100 |
| Message-ID | <bsh60a-dn.ln1@satorlaser.homedns.org> |
| In reply to | #40236 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-03-01 08:00 -0500 |
| Message-ID | <mailman.2719.1362142804.2939.python-list@python.org> |
| In reply to | #40247 |
[Multipart message — attachments visible in raw view] — view raw
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
[toc] | [prev] | [next] | [standalone]
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 08:28 -0800 |
| Message-ID | <4d52aac9-d5f0-4124-982c-a4bcf2bc98a4@googlegroups.com> |
| In reply to | #40247 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2013-03-04 11:08 +0100 |
| Message-ID | <ge8e0a-26m.ln1@satorlaser.homedns.org> |
| In reply to | #40270 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-02 00:41 +1100 |
| Message-ID | <mailman.2720.1362145274.2939.python-list@python.org> |
| In reply to | #40236 |
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
[toc] | [prev] | [next] | [standalone]
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 08:14 -0800 |
| Message-ID | <ebbe17b1-f007-4614-bc9b-0cfffa998008@googlegroups.com> |
| In reply to | #40250 |
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
[toc] | [prev] | [next] | [standalone]
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 08:14 -0800 |
| Message-ID | <mailman.2736.1362154451.2939.python-list@python.org> |
| In reply to | #40250 |
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
[toc] | [prev] | [next] | [standalone]
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 08:18 -0800 |
| Message-ID | <f921f5f2-4f67-4fa2-a206-f700cabaead0@googlegroups.com> |
| In reply to | #40250 |
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
[toc] | [prev] | [next] | [standalone]
| From | Isaac Won <winefrog@gmail.com> |
|---|---|
| Date | 2013-03-01 08:18 -0800 |
| Message-ID | <mailman.2737.1362154743.2939.python-list@python.org> |
| In reply to | #40250 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web