Function with varying number of For Loops (python)

倾然丶 夕夏残阳落幕 提交于 2019-11-26 17:43:01

This problem can be solved by recursion. I am just writing an algorithm here, since I believe this can be a general problem.

function Recurse (y, number) 
   if (number > 1)
      Recurse ( y, number - 1 )
   else
      for x in range (y)
      whatever()

I'm not clear why you can't use the product of the bounds and do

for x in range(y exp n)

where n is the # of loops.... You say y exp n will be huge, but I'm sure python can handle it.

However, that being said, what about some sort of recursive algorithm?

def loop_rec(y, n):
    if n >= 1:
        for x in range(y):
            loop_rec(y, n - 1)
    else:
       whatever()

this can be done without recursion using itertools.product

import itertools
def function(n):
    for x in itertools.product(range(n),repeat=n):
        whatever()

Recursion will be your best bet. Consider what it should do in the base case and in the recursive case.

Code left out, as per request.

Here you go. Let ranges be your ranges, operate on result when you need to.

ranges=((1,4),(0,3),(3,6))
from operator import mul
operations=reduce(mul,(p[1]-p[0] for p in ranges))-1
result=[i[0] for i in ranges]
pos=len(ranges)-1
increments=0
print result
while increments < operations:
    if result[pos]==ranges[pos][1]-1:
        result[pos]=ranges[pos][0]
        pos-=1
    else:
        result[pos]+=1
        increments+=1
        pos=len(ranges)-1 #increment the innermost loop
        print result

[1, 0, 3]
[1, 0, 4]
[1, 0, 5]
[1, 1, 3]
[1, 1, 4]
[1, 1, 5]
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[2, 0, 3]
[2, 0, 4]
[2, 0, 5]
[2, 1, 3]
[2, 1, 4]
[2, 1, 5]
[2, 2, 3]
[2, 2, 4]
[2, 2, 5]
[3, 0, 3]
[3, 0, 4]
[3, 0, 5]
[3, 1, 3]
[3, 1, 4]
[3, 1, 5]
[3, 2, 3]
[3, 2, 4]
[3, 2, 5]
[1, 0, 4]

Testing with the following would give the same result:

for x in range(*ranges[0]):
    for y in range(*ranges[1]):
        for z in range(*ranges[2]):
            print [x,y,z]

Have you considered xrange ?

for x in xrange(y ** n):
    whatever()

And if you overshoot even xrange limit, you can use itertool

import itertools
for x in itertools.product(xrange(y), repeat=n):
   whatever()

(previous itertool answer incorrectly used n for the range instead of y)

My reply is late, but supposing that you want to do multiple loops, e.g. print some range multiple times. Then the correct version of this recursion is:

def loop_rec(y, number):
   if (number > 1):
      loop_rec( y, number - 1 )
      for i in range(y): 
         print(i, end=' ')        
   else:      
      for i in range(y):
         print(i, end=' ')

loop_rec(4,3)

This will create three for loops with the range(4)

If you want to play around with dynamic range, here are some variants:

def loop_rec(y, number):
if (number > 1):
    loop_rec( y+1, number - 1 )
    for i in range(y): 
        print(i, end=' ')
    print(' ;')
else:      
    for i in range(y):
        print(i, end=' ')
    print(';')

loop_rec(6,4)

which will print out:

0 1 2 3 4 5 6 7 8 ;
0 1 2 3 4 5 6 7  ;
0 1 2 3 4 5 6  ;
0 1 2 3 4 5  ;

or

def loop_rec(y, number):
if (number > 1):
    loop_rec( y-1, number - 1 )
    for i in range(y): 
        print(i, end=' ')
    print(' ;')
else:      
    for i in range(y):
        print(i, end=' ')
    print(';')
loop_rec(6,4)

which will output:

0 1 2 ;
0 1 2 3  ;
0 1 2 3 4  ;
0 1 2 3 4 5  ;

A better variant which is using only one for loop (less typing) is the following:

def loop_rec(y, number):
    if (number >= 1):
        loop_rec( y+1, number - 1 )
        for i in range(y): 
            print(i, end=' ')
        print('')
    else:      
        return

loop_rec(1,5)

will output:

0 1 2 3 4 
0 1 2 3 
0 1 2 
0 1 
0 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!