Re: looking for a neat solution to a nested loop problem
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) I'll contrubute another version by the xrange function in python. for i in xrange(N, N+100): # not list based if i100: i2=i else: i2=i-100 # not in the inner most loop for j in xrange(M, M+100): if j100: j2=j else: j2=j-100 do_something(i2,j2) -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) list(range(N,100))+ list(range(0,N)) //good for j Well, this is the way! -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)) list(range(N,100))+list(range(1,N)) //good for i I contribute my python code here to avoid any divison. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j) This is a good example to be tuned into some example such that this kind of loops by iterators of parameters in python wich do not use any division at all. But I am getting lazy recently for non-critical parts. -- http://mail.python.org/mailman/listinfo/python-list
looking for a neat solution to a nested loop problem
consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
Are you familiar with the itertools module? itertools.product is designed for this purpose: http://docs.python.org/library/itertools#itertools.product Oscar. On 6 August 2012 16:52, Tom P werot...@freent.dd wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. -- http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 6 August 2012 16:52, Tom P werot...@freent.dd wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. -- http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list Are you familiar with the itertools module? itertools.product is designed for this purpose: http://docs.python.org/library/itertools#itertools.product -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. for i in range(N, N+100): for j in range(M, M+100): do_something(i, j) ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
In a8a7hvf8c...@mid.individual.net Tom P werot...@freent.dd writes: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. You could define your own generator function that yields values in whatever order you want: def my_generator(): yield 9 yield 100 for i in range(200, 250): yield i yield 5 -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, The Gashlycrumb Tinies -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
The function range can be called with more than one argument. For example: for i in range(N, N + 10): for j in range(M, M + 100): do_something(i, j) You can also call range with 3 arguments, if want a step size different to 1: for k in range(2, 11, 3): print(k) 2 5 8 Hope this is clear, Ian On 06/08/12 16:52, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? Alternatively: import itertools for i, j in itertools.product(range(N,N+100),range(M,M+100)): do_something(i,j) This can be preferable to deeply-nested loops. Also: in 2.x, use xrange() in preference to range(). -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) .. apologies if I didn't make that clear enough. Alternatively: import itertools for i, j in itertools.product(range(N,N+100),range(M,M+100)): do_something(i,j) This can be preferable to deeply-nested loops. Also: in 2.x, use xrange() in preference to range(). -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 08/06/2012 06:03 PM, John Gordon wrote: In a8a7hvf8c...@mid.individual.net Tom P werot...@freent.dd writes: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. You could define your own generator function that yields values in whatever order you want: def my_generator(): yield 9 yield 100 for i in range(200, 250): yield i yield 5 Thanks, I'll look at that but I think it just moves the clunkiness from one place in the code to another. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 6 August 2012 18:14, Tom P werot...@freent.dd wrote: On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) .. apologies if I didn't make that clear enough. How about range(N, 100) + range(0, N)? Example (Python 2.x): range(3, 10) [3, 4, 5, 6, 7, 8, 9] range(0, 3) [0, 1, 2] range(3, 10) + range(0, 3) [3, 4, 5, 6, 7, 8, 9, 0, 1, 2] In Python 3.x you'd need to do list(range(...)) + list(range(...)) or use itertools.chain. Oscar -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 8/6/2012 10:14 AM Tom P said... On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) .. apologies if I didn't make that clear enough. for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
* Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. you mean: do_something((i + N) % 100, (j + M) % 100) ? I'd define my own range function doing exactly that. def rrange(count, start=0): for j in xrange(count): yield (j + start) % count (untested) Or use some itertools magic for that. It might be faster. nd -- Umfassendes Werk (auch fuer Umsteiger vom Apache 1.3) -- aus einer Rezension http://pub.perlig.de/books.html#apache2 -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 2012-08-06, Grant Edwards invalid@invalid.invalid wrote: On 2012-08-06, Tom P werot...@freent.dd wrote: On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) In 2.x: for i in range(M,100)+range(0,M): for j in range(N,100)+range(0,N): do_something(i,j) Dunno if that still works in 3.x. I doubt it, since I think in 3.x range returns an iterator, not? Indeed it doesn't work in 3.x, but this does: from itertools import chain for i in chain(range(M,100),range(0,M)): for j in chain(range(N,100),range(0,N)): do_something(i,j) -- Grant Edwards grant.b.edwardsYow! People humiliating at a salami! gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 2012-08-06, Tom P werot...@freent.dd wrote: On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) In 2.x: for i in range(M,100)+range(0,M): for j in range(N,100)+range(0,N): do_something(i,j) Dunno if that still works in 3.x. I doubt it, since I think in 3.x range returns an iterator, not? -- Grant Edwards grant.b.edwardsYow! I wish I was on a at Cincinnati street corner gmail.comholding a clean dog! -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 08/06/2012 08:29 PM, Grant Edwards wrote: On 2012-08-06, Grant Edwards invalid@invalid.invalid wrote: On 2012-08-06, Tom P werot...@freent.dd wrote: On 08/06/2012 06:18 PM, Nobody wrote: On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) In 2.x: for i in range(M,100)+range(0,M): for j in range(N,100)+range(0,N): do_something(i,j) Dunno if that still works in 3.x. I doubt it, since I think in 3.x range returns an iterator, not? Indeed it doesn't work in 3.x, but this does: from itertools import chain for i in chain(range(M,100),range(0,M)): for j in chain(range(N,100),range(0,N)): do_something(i,j) ah, that looks good - I guess it works in 2.x as well? -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 2012-08-06, Tom P werot...@freent.dd wrote: no, I meant something else .. j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) In 2.x: for i in range(M,100)+range(0,M): for j in range(N,100)+range(0,N): do_something(i,j) Dunno if that still works in 3.x. I doubt it, since I think in 3.x range returns an iterator, not? Indeed it doesn't work in 3.x, but this does: from itertools import chain for i in chain(range(M,100),range(0,M)): for j in chain(range(N,100),range(0,N)): do_something(i,j) ah, that looks good - I guess it works in 2.x as well? I don't know. Let me test that for you... $ python Python 2.6.8 (unknown, May 18 2012, 11:56:26) [GCC 4.5.3] on linux2 Type help, copyright, credits or license for more information. from itertools import chain for i in chain(range(0,5),range(5,10)): ... print i ... 0 1 2 3 4 5 6 7 8 9 Yes, it works in 2.x as well. -- Grant Edwards grant.b.edwardsYow! ... bleakness at ... desolation ... plastic gmail.comforks ... -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 8/6/2012 12:22 PM Grant Edwards said... On 2012-08-06, Tom P werot...@freent.dd wrote: snip ah, that looks good - I guess it works in 2.x as well? I don't know. Let me test that for you... snip Yes, it works in 2.x as well. :) And from the docs, all the way back to 2.3! 9.7. itertools Functions creating iterators for efficient looping New in version 2.3. Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 6 August 2012 16:52, Tom P werot...@freent.dd wrote: consider a nested loop algorithm - for i in range(100): for j in range(100): do_something(i,j) Now, suppose I don't want to use i = 0 and j = 0 as initial values, but some other values i = N and j = M, and I want to iterate through all 10,000 values in sequence - is there a neat python-like way to this? I realize I can do things like use a variable for k in range(1): and then derive values for i and j from k, but I'm wondering if there's something less clunky. For example: for i in range(100): for j in range(100): do_something((i + N)%100, (j + N)%100) Cheers, -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On Mon, 06 Aug 2012 19:16:45 +0200, Tom P wrote: def my_generator(): yield 9 yield 100 for i in range(200, 250): yield i yield 5 Thanks, I'll look at that but I think it just moves the clunkiness from one place in the code to another. And if there was a built-in command that did exactly what you wanted, it too would also move the clunkiness from one place in the code to another. What you are asking for is clunky: [quote] j runs through range(M, 100) and then range(0,M), and i runs through range(N,100) and then range(0,N) [end quote] There's no magic pixie dust that you can sprinkle on it to make it elegant. Assuming M and N are small (under 100), you can do this: values = range(100) # or list(range(100)) in Python 3. for j in (values[M:] + values[:M]): for i in (values[N:] + values[:N]): ... which isn't too bad. If you have to deal with much large ranges, you can use itertools to chain them together: import itertools jvalues = itertools.chain(xrange(M, 100), xrange(M)) # or just range ivalues = itertools.chain(xrange(N, 250), xrange(N)) # in Python 3 for j in jvalues: for i in ivalues: ... -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a neat solution to a nested loop problem
On 08/06/2012 11:11 AM, Emile van Sebille wrote: snip for i in range(N,N+100): for j in range(M,M+100): do_something(i % 100 ,j % 100) Emile How about... for i in range(100): for j in range(100): do_something((i + N) % 100, (j + M) % 100) -=- Larry -=- -- http://mail.python.org/mailman/listinfo/python-list
Loop problem while generating a new value with random.randint()
Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza in your script you generate the random number only once, no wonder it keep being the same value over over. # Initialize ranum with a random number ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ranum = random.randint(1,100) # generate a new number, that's the missing line in your script JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Jean-Michel Pichavant a écrit : Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. (snip - problem already addressed by Jean-Michel...) while var != ranum: if var == ranum: Note that this branch will never be executed - the condition in the while statement make sure var != ranum in the while block. print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Jean-Michel Pichavant jeanmic...@sequans.com writes: Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza in your script you generate the random number only once, no wonder it keep being the same value over over. # Initialize ranum with a random number ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ranum = random.randint(1,100) # generate a new number, that's the missing line in your script JM And a more idiomatic way of writing this in Python would be, I guess: import random var = 65 while True: ranum = random.randint(1, 100) if var == ranum: print var, 'equals to:', ranum break else: print var, 'does not equal to:', ranum (following up from a FU as I can't see the OP) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: The if condition can never be true, since if it were then the loop would have just terminated before starting this iteration! print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum Shouldn't there be something here to set ranum to a new value? Otherwise the same value will be there the next time around ... ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ d -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Thanks for the help! Using while True helps alot! Paulo Repreza -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
On Aug 13, 8:46 am, Sion Arrowsmith [EMAIL PROTECTED] wrote: Dave [EMAIL PROTECTED] wrote: hitNum = 0 stopCnt = 6 + hitNum offSet = 5 for i in range(0,10,1): The step argument to range defaults to 1: it's tidier to omit it. Similarly, the start argument defaults to 0, so you can drop that too. for i in range(10): for x in range(hitNum,len(inLst), 1): print hitNum, stopCnt hitNum and stopCnt are constant in this loop: if you care about this print statement, move it into the outer loop and stop yourself drowning in output. if x == stopCnt: break If you want to exit the inner loop when x == stopCnt, why not make that condition part of the loop construct? for x in range(hitNum, stopCnt): That said, if you ever see for i in range(len(lst)) *immediately* replace it by for i, x in enumerate(lst), then go through to body to see if you really need that i, and if not use for x in lst, with slicing if the range is more complex than range(len(lst)). As you can do here: for x in inLst[hitNum:stopCnt]: hitLst.append(x) And if all you're doing in a for loop is appending to one list from another, that's just what list.extend does: hitLst.extend(inLst[hitNum:stopCnt]) hitNum +=offSet stopCnt+=offSet Finally, that i in the outer loop isn't being used anywhere. Why don't you just loop over hitNum? And noticing that stopCnt is increasing in lock-step with hitNum: offset = 5 for hitNum in range(0, 10*offset, offset): hitLst.extend(inLst[hitNum:hitNum+6] -- \S -- [EMAIL PROTECTED] --http://www.chaos.org.uk/~sion/ Frankly I have no feelings towards penguins one way or the other -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump Excellent, thanks for all your help guys. -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
arrrggg, now I feel really dumb.. hitNum = 0 stopCnt = 6 + hitNum offSet = 5 for i in range(0,10,1): for x in range(hitNum,len(inLst), 1): print hitNum, stopCnt if x == stopCnt: break hitLst.append(inLst[x]) hitNum +=offSet stopCnt+=offSet print hitLst Beers, Dave On Aug 13, 12:58 am, Dave [EMAIL PROTECTED] wrote: On Aug 13, 12:35 am, Larry Bates [EMAIL PROTECTED] wrote: Dave wrote: Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave Dave, You are going to need to supply us with more info to help. 1) Are you always going to want to get 6 elements from the list 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? 3) Is your requirement just to split inLst into equal length lists and append them together into a list? Depending on your answers this might be quite easy. -Larry Hi Larry, well to answer your questions. 1) Are you always going to want to get 6 elements from the list yes for this example, but this will change depending on the length of the sig. hitNum will be changing depending on what element I need to lag ie: if hitNum = 1 ['b1', 'c1', 'd1', 'e1', 'a2', 'b2'] so I will be lagging the b elements. 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? I think I just answered this 3) Is your requirement just to split inLst into equal length lists and append them together into a list? yes Thanks for all your help, Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
Dave napisał(a): Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave It seems you're going through a lot of trouble just to construct the list by hand. Why don't you try slicing? d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 in_lst = d.split() hit_lst = in_lst[0:6] hit_lst_2 = in_lst[5:11] Regards, Marek -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
Dave wrote: arrrggg, now I feel really dumb.. hitNum = 0 stopCnt = 6 + hitNum offSet = 5 for i in range(0,10,1): for x in range(hitNum,len(inLst), 1): print hitNum, stopCnt if x == stopCnt: break hitLst.append(inLst[x]) hitNum +=offSet stopCnt+=offSet print hitLst Beers, Dave On Aug 13, 12:58 am, Dave [EMAIL PROTECTED] wrote: On Aug 13, 12:35 am, Larry Bates [EMAIL PROTECTED] wrote: Dave wrote: Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave Dave, You are going to need to supply us with more info to help. 1) Are you always going to want to get 6 elements from the list 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? 3) Is your requirement just to split inLst into equal length lists and append them together into a list? Depending on your answers this might be quite easy. -Larry Hi Larry, well to answer your questions. 1) Are you always going to want to get 6 elements from the list yes for this example, but this will change depending on the length of the sig. hitNum will be changing depending on what element I need to lag ie: if hitNum = 1 ['b1', 'c1', 'd1', 'e1', 'a2', 'b2'] so I will be lagging the b elements. 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? I think I just answered this 3) Is your requirement just to split inLst into equal length lists and append them together into a list? yes Thanks for all your help, Dave This can also be written as: d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 \ a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() sliceLen = 6 step = 5 offset = 1 hitLst = list() for n in xrange(len(inLst)/step): start = offset + n * step hitLst.append(inLst[offset:offset + sliceLen]) print hitLst -Larry -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
Dave [EMAIL PROTECTED] wrote: hitNum = 0 stopCnt = 6 + hitNum offSet = 5 for i in range(0,10,1): The step argument to range defaults to 1: it's tidier to omit it. Similarly, the start argument defaults to 0, so you can drop that too. for i in range(10): for x in range(hitNum,len(inLst), 1): print hitNum, stopCnt hitNum and stopCnt are constant in this loop: if you care about this print statement, move it into the outer loop and stop yourself drowning in output. if x == stopCnt: break If you want to exit the inner loop when x == stopCnt, why not make that condition part of the loop construct? for x in range(hitNum, stopCnt): That said, if you ever see for i in range(len(lst)) *immediately* replace it by for i, x in enumerate(lst), then go through to body to see if you really need that i, and if not use for x in lst, with slicing if the range is more complex than range(len(lst)). As you can do here: for x in inLst[hitNum:stopCnt]: hitLst.append(x) And if all you're doing in a for loop is appending to one list from another, that's just what list.extend does: hitLst.extend(inLst[hitNum:stopCnt]) hitNum +=offSet stopCnt+=offSet Finally, that i in the outer loop isn't being used anywhere. Why don't you just loop over hitNum? And noticing that stopCnt is increasing in lock-step with hitNum: offset = 5 for hitNum in range(0, 10*offset, offset): hitLst.extend(inLst[hitNum:hitNum+6] -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ Frankly I have no feelings towards penguins one way or the other -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
newb loop problem
Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
Dave wrote: Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave Dave, You are going to need to supply us with more info to help. 1) Are you always going to want to get 6 elements from the list 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? 3) Is your requirement just to split inLst into equal length lists and append them together into a list? Depending on your answers this might be quite easy. -Larry -- http://mail.python.org/mailman/listinfo/python-list
Re: newb loop problem
On Aug 13, 12:35 am, Larry Bates [EMAIL PROTECTED] wrote: Dave wrote: Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5 d5 e5 inLst = d.split() hitLst = [] hitNum = 0 stopCnt = 6 + hitNum for i in range(hitNum,len(inLst), 1): if i == stopCnt: break hitLst.append(inLst[i]) print hitLst -- $ python helper.py ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] This works fine for my purposes, what I need is an outer loop that goes through the original list again and appends my next request. ie. hitNum = 5 which will return: ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] and append it to the previous one. ie: ['a1', 'b1', 'c1', 'd1', 'e1', 'a2'] ['a2', 'b2', 'c2', 'd2', 'e2', 'a3'] not really sure how to do this right now though, been trying several methods with no good results. btw, just creating lagged values (sort of shift registers) on the incoming signal Many thanks, Dave Dave, You are going to need to supply us with more info to help. 1) Are you always going to want to get 6 elements from the list 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? 3) Is your requirement just to split inLst into equal length lists and append them together into a list? Depending on your answers this might be quite easy. -Larry Hi Larry, well to answer your questions. 1) Are you always going to want to get 6 elements from the list yes for this example, but this will change depending on the length of the sig. hitNum will be changing depending on what element I need to lag ie: if hitNum = 1 ['b1', 'c1', 'd1', 'e1', 'a2', 'b2'] so I will be lagging the b elements. 2) Are you always going to want to step by 5 elements as your offset each time through the outer loop? I think I just answered this 3) Is your requirement just to split inLst into equal length lists and append them together into a list? yes Thanks for all your help, Dave -- http://mail.python.org/mailman/listinfo/python-list
loop problem
Hi, First, I want to thank those who responded to my question about "the plot module" yesterday. I realize now that the question could have been vague. There is a plot module, simply called "plot," that I am using, and I guess it is not a very widely circulated plotting utility. Anyway, I'm a little confused by the output that I get from the following code: /***/ from Tkinter import *from tkSimpleDialog import *import Pmwimport HP34401Aimport plotimport time class Application(Frame): def __init__(self,master=None): Frame.__init__(self,master) self.pack() self.graph = plot.plot(self) self.beginplotting() def beginplotting(self): x = [] y = [] i = 0 fLastOutput = time.time() while 1: fTime = time.time() if((fTime-fLastOutput) = 2): i += 1 x.append(fTime) y.append(2*fTime) self.graph.addSet(x,y) fLastOutput = fTime if(i=30):break app = Application()app.mainloop() /***/ This is supposed to plot a point every two seconds. When it actually runs, it instead stores all the data points and then plots them after 30 data points have been taken. I'm fairly certain that this is not due to the use of the plot class. If I have the program insteadwrite each time into a Tkinter Text widget, the same type of thing happens. That is, the program stores all 30 times in memory and then dumps them into the Text widget at the end. Since I have written the code so that it plots (or writes to the Text widget) every two seconds, I am confused as to why this is happening. Shankar -- http://mail.python.org/mailman/listinfo/python-list
Re: loop problem
Hi, First ;-) I don't know Tk or plot, but from looking at your code I see you want to plot a line a minute long, after that, all dots are drawn to the screen. You call self.graph.addSet(x,y) My guess is that you have to explicitly draw to the screen using same call from graph or self? whatever Tkinter wants. Search the tkinter/tk docs for 'draw' calls. At 16:18 -0400 13/7/2005, Shankar Iyer ([EMAIL PROTECTED]) wrote: Content-Language: en Content-Type: text/html; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 7bit Hi, First, I want to thank those who responded to my question about the plot module yesterday. I realize now that the question could have been vague. There is a plot module, simply called plot, that I am using, and I guess it is not a very widely circulated plotting utility. Anyway, I'm a little confused by the output that I get from the following code: /***/ from Tkinter import * from tkSimpleDialog import * import Pmw import HP34401A import plot import time class Application(Frame): def __init__(self,master=None): Frame.__init__(self,master) self.pack() self.graph = plot.plot(self) self.beginplotting() def beginplotting(self): x = [] y = [] i = 0 fLastOutput = time.time() while 1: fTime = time.time() if((fTime-fLastOutput) = 2): i += 1 x.append(fTime) y.append(2*fTime) self.graph.addSet(x,y) fLastOutput = fTime if(i=30):break app = Application() app.mainloop() /***/ This is supposed to plot a point every two seconds. When it actually runs, it instead stores all the data points and then plots them after 30 data points have been taken. I'm fairly certain that this is not due to the use of the plot class. If I have the program instead write each time into a Tkinter Text widget, the same type of thing happens. That is, the program stores all 30 times in memory and then dumps them into the Text widget at the end. Since I have written the code so that it plots (or writes to the Text widget) every two seconds, I am confused as to why this is happening. Shankar -- http://mail.python.org/mailman/listinfo/python-list -- Vriendelijke groet, Paul -- http://mail.python.org/mailman/listinfo/python-list