[Tutor] Memory and functions
I have a rather obscure question, and not sure where in the docs to find an answer like this. For functions that return values (ie the list method pop): If you don't assign the returned value to anything does it still end up residing in memory? Just seems like this could be a potential problem to watch out for when memory usage is an issue (probably not most of the time). Also, where can I begin to find these 'lower level' answers in the docs? Thanks! Charles ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random order program
Dave, Myles, et al, On Nov 27, 2011, at 4:25 PM, Dave Angel d...@davea.name wrote: On 11/27/2011 05:17 PM, myles broomes wrote: #random order list while len(random_word_list) != len(word_list): word = random.choice(word_list) if word not in random_word_list: random_word_list += word If you use += operator with list on the left side, it assumes something compatible with list on the right. So either use random_word_list += [word] Or else use random_word_list.append(word) Everyone has offered some good feedback, I just wanted to throw in this, and hopefully everyone can say if I'm correct or not: A way to make the code more 'pythonic' and easier to read might be to replace the conditional while len(random_word_list) != len(word_list) With the following : For x in range(len(word_list)) This will prevent infinite loops, easier to read, and allows for a lot of other uses (even if x is never used). Any thoughts people? And would this method (on a small or large scale) be 'cheaper' than the original conditional? Or more 'pythonic'? Charles Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Sue, I'm not familiar with FORTRAN, and since I'm not completely sure what you're trying to accomplish please take this simply as an 'educated guess'. My guess is that you're not getting the curve because Z is only defined to one decimal location (1/10th) precision and probably needs higher precision (and is likely getting it) in the FORTRAN version. http://docs.python.org/tutorial/floatingpoint.html this should help Charles Sent from my iPhone On Nov 25, 2011, at 2:16 AM, stm atoc stm.at...@googlemail.com wrote: Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do write(0,*) 'z ', z(1:N) write(0,*) 'when ', 'nu ','Conc ' do i= 1, N nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s nu(i)= num(1) + nuh(i) end do -- My problem is I am notable have the curve in the output plot as I have as a result of FORTRAN program. What happens is just having a straight line! the whole problem is in z part, which is supposed to be changed and i do not see it! So, would it be possible to take a look at it please. any suggestion would greatly appreciated. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on List Comprehensions
Alan, Steve, future readers, After some re-reading and hacking I was able to discover the solution. Since I raised the question here it is : [['{0}'.format(x+1), x+1] for x in range(size)] This will create the list with nested lists for whatever number 'size' is set to. This should be good enough to get anyone started on future problems in this area, and yes the redundancy present does make sense in the scope of what I'm working on. :) Thanks! I'll leave future questions on the backburner a little longer. Charles Sent from my iPhone On Nov 21, 2011, at 7:04 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 22/11/11 00:10, Steven D'Aprano wrote: Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: listcomp = [expression for name in sequence] not listcomp = [expression for name in sequence another_command] And being picky you can add a conditional after the loop: listcomp = [expression for name in sequence if some_condition] But it must be an if test, nothing else will do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor