looking for a neat solution to a nested loop problem

Ian Foote ian at feete.org
Mon Aug 6 12:07:19 EDT 2012


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.




More information about the Python-list mailing list