[Tutor] Do loop in Python

Andreas Perstinger andreas.perstinger at gmx.net
Fri Nov 25 22:44:00 CET 2011


On 2011-11-25 14:46, stm atoc wrote:
> Here is the new version of the program:
>
> zvalues = [-200]  # starting value
> hvalues = [10]  # starting value
> increments = [1, 1, 1, 1, 1, 1, 1, 1]
> for N in increments:
>         h = hvalues[-1] - N
>         hvalues.append(h)
>         z = zvalues[-1] + h
>         zvalues.append(z)
>         height = arange((z)*dz,0,dz)

There is no "arange" in python. Could it be that you use numpy and 
import it with "from numpy import *"?

>         for z,when in enumerate(height):

I'm pretty sure this line doesn't do what you expect it to do. You have 
a sequence (a numpy array) named "height" and after calling "enumerate" 
you get a list of tuples in the form of [(0, height[0]), (1, height[1]), 
...]. Now the for-loop iterates over this list and assigns "z" to the 
first value of the tuple (the index-values) and "when" to the second 
(the values from "height"). You later never use "when" but just use "z". 
If you really want that, the "enumerate" is completly unnecessary and 
you could just use "for z in range(len(height))". But I'm not sure if 
numpy arrays work with "len()".

>             nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence
> diffusivity m**2/s
>             nu.append(num + nuh[z])
>
> The story is like this:
> I should define layers and thickness and see how the diffusion profile
> changes over the z.
> height (or depth) of the total thickness or 'z'.
> I basically, define 'z' in 10 layers and each layer is called  ' N' .
> Difference between each layer is 'h', which is equal 10 micrometer.
> Now, what I like to do is the modification of nu based on each zvalue
> In fact, for each 'zvalue' o'z' step, I need to calculate a different
> value for 'nu' based on the available equation in the program.
>
> BUT, I am not sure, exactly, how to add the new do loop of z inside
> another loop of nu.

For me your explanations are still too confusing. Could it be that you 
are thinking way too complicated?

My guess is you want to have a range of material thicknesses (from 1 to 
200 micrometers in 10 micrometer-steps) and then you want from each 
thickness 10 different layers, right?

import math # you should always tell us which modules you import
num = 0.05 # some constant
nu = [] # list of resulting values
h = 10.0 # height of one layer
thickness = range(0, 210, 10) # a list from 0 to 200 with step 10 (0, 
10, 20, ..., 190, 200)
layers = range(1,11) # a list from 1 to 10
for t in thickness:
   for l in layers:
     z = t + h * l # I'm not sure if you want to add or subtract the 
layer thickness
     nu = num + (0.01 * math.exp(-0.05 * (z + 200.0)))

This will result in a big one-dimensional list where you calculate for 
each thickness the nu-value for 10 layers. Am I close?
I'm still not sure about the steps and the height of the layers. I also 
wonder if it wouldn't be better to use a two-dimensional list.

> I have done this way as well (the other way around):
>
> height = arange((z)*dz,0,dz)
> for z,when in enumerate(height):
>      for N in increments:
>         h = hvalues[-1] - N
>         hvalues.append(h)
>         z = zvalues[-1] + h
>         zvalues.append(z)
>         nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence
> diffusivity m**2/s
>         nu.append(num + nuh[z])
>
> but still no sign of 'nu changes' over 'z'!

As Charles has already mentioned, the values for "nu" are very similar 
(they start beginning to differ just at the seventh digit after the 
comma). How do you further process this values? If you plot them what's 
your scale?

Bye, Andreas


More information about the Tutor mailing list