# looking for a neat solution to a nested loop problem

Tue Aug 14 23:08:46 CEST 2012

```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
>
> >
>
>
> >
>
> > 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)

```