looking for a neat solution to a nested loop problem

88888 Dihedral dihedral88888 at googlemail.com
Thu Aug 9 14:39:56 EDT 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
> 
> > 
> 
> > 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.



More information about the Python-list mailing list