Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

looking for a neat solution to a nested loop problem

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


werotizy at freent

Aug 6, 2012, 8:52 AM

Post #1 of 25 (950 views)
Permalink
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(10000): 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


oscar.benjamin at bristol

Aug 6, 2012, 9:00 AM

Post #2 of 25 (940 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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 <werotizy [at] freent> 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(10000): 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<http://mail.python.org/mailman/listinfo/python-list>
>


oscar.j.benjamin at gmail

Aug 6, 2012, 9:02 AM

Post #3 of 25 (939 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 6 August 2012 16:52, Tom P <werotizy [at] freent> 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(10000): 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<http://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


gordon at panix

Aug 6, 2012, 9:03 AM

Post #4 of 25 (943 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

In <a8a7hvF8crU1 [at] mid> Tom P <werotizy [at] freent> 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(10000): 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
gordon [at] panix B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

--
http://mail.python.org/mailman/listinfo/python-list


ian at feete

Aug 6, 2012, 9:07 AM

Post #5 of 25 (943 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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(10000): 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


ethan at stoneleaf

Aug 6, 2012, 9:15 AM

Post #6 of 25 (942 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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(10000): 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


nobody at nowhere

Aug 6, 2012, 9:18 AM

Post #7 of 25 (942 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


werotizy at freent

Aug 6, 2012, 10:14 AM

Post #8 of 25 (939 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


werotizy at freent

Aug 6, 2012, 10:16 AM

Post #9 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 08/06/2012 06:03 PM, John Gordon wrote:
> In <a8a7hvF8crU1 [at] mid> Tom P <werotizy [at] freent> 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(10000): 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


oscar.benjamin at bristol

Aug 6, 2012, 10:35 AM

Post #10 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 6 August 2012 18:14, Tom P <werotizy [at] freent> 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


emile at fenx

Aug 6, 2012, 11:11 AM

Post #11 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


ndparker at gmail

Aug 6, 2012, 11:19 AM

Post #12 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

* 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(10000): 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


invalid at invalid

Aug 6, 2012, 11:25 AM

Post #13 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 2012-08-06, Tom P <werotizy [at] freent> 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.edwards Yow! I wish I was on a
at Cincinnati street corner
gmail.com holding a clean dog!
--
http://mail.python.org/mailman/listinfo/python-list


invalid at invalid

Aug 6, 2012, 11:29 AM

Post #14 of 25 (941 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 2012-08-06, Grant Edwards <invalid [at] invalid> wrote:
> On 2012-08-06, Tom P <werotizy [at] freent> 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.edwards Yow! People humiliating
at a salami!
gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


werotizy at freent

Aug 6, 2012, 12:03 PM

Post #15 of 25 (937 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 08/06/2012 08:29 PM, Grant Edwards wrote:
> On 2012-08-06, Grant Edwards <invalid [at] invalid> wrote:
>> On 2012-08-06, Tom P <werotizy [at] freent> 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


invalid at invalid

Aug 6, 2012, 12:22 PM

Post #16 of 25 (920 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 2012-08-06, Tom P <werotizy [at] freent> 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.edwards Yow! ... bleakness
at ... desolation ... plastic
gmail.com forks ...
--
http://mail.python.org/mailman/listinfo/python-list


emile at fenx

Aug 6, 2012, 12:52 PM

Post #17 of 25 (923 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 8/6/2012 12:22 PM Grant Edwards said...
> On 2012-08-06, Tom P <werotizy [at] freent> 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


arnodel at gmail

Aug 6, 2012, 1:14 PM

Post #18 of 25 (923 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

On 6 August 2012 16:52, Tom P <werotizy [at] freent> 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(10000): 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


steve+comp.lang.python at pearwood

Aug 6, 2012, 6:27 PM

Post #19 of 25 (917 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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, 1000000), xrange(M)) # or just range
ivalues = itertools.chain(xrange(N, 2500000), xrange(N)) # in Python 3
for j in jvalues:
for i in ivalues:
...



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


orgnut at yahoo

Aug 6, 2012, 9:02 PM

Post #20 of 25 (917 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


nobody at nowhere

Aug 7, 2012, 8:32 AM

Post #21 of 25 (917 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


dihedral88888 at googlemail

Aug 7, 2012, 6:42 PM

Post #22 of 25 (918 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


dihedral88888 at googlemail

Aug 9, 2012, 11:39 AM

Post #23 of 25 (899 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


dihedral88888 at googlemail

Aug 9, 2012, 11:46 AM

Post #24 of 25 (899 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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


dihedral88888 at googlemail

Aug 14, 2012, 2:08 PM

Post #25 of 25 (865 views)
Permalink
Re: looking for a neat solution to a nested loop problem [In reply to]

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 i<100: i2=i else: i2=i-100 # not in the inner most loop
for j in xrange(M, M+100):
if j<100: j2=j else: j2=j-100
do_something(i2,j2)



--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.