Python Forum
RecursionError: maximum recursion depth exceeded in comparison ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: RecursionError: maximum recursion depth exceeded in comparison ? (/thread-16790.html)

Pages: 1 2


RecursionError: maximum recursion depth exceeded in comparison ? - leoahum - Mar-14-2019

def ChangeList (K = [0], itr = 0):
    if itr < 10:
        K.append(1)
        itr += 1
        ChangeList()

    else:
        return K


L = ChangeList()

print (L)
I'm trying to get a list from the script above, but got an error "maximum recursion depth exceeded in comparison".
Can anyone help me fix it? It is an iteration practice, I understand there're different ways to get the list.


RE: Iteration Question - Yoriz - Mar-14-2019

def ChangeList (K = [0], itr = 0):
    if itr < 10:
        K.append(1)
        itr += 1
        return ChangeList(K, itr)
 
    else:

        return K
 
 
L = ChangeList()
 
print (L)
Output:
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]



RE: RecursionError: maximum recursion depth exceeded in comparison ? - micseydel - Mar-15-2019

You're using recursion. When you have recursion, you have two situations: a "base case" (where you don't recurse) and a recursive case. The error you're encountering is incredibly common when the base case isn't solid. I suggest you not just use Yoriz' code, but rather you walk through your own code and then his in order to understand what's happening.

Also, this looks like homework, which often has weird requirements, but I wouldn't use recursion for what you're trying to do there.


RE: RecursionError: maximum recursion depth exceeded in comparison ? - leoahum - Mar-15-2019

(Mar-15-2019, 12:11 AM)micseydel Wrote: You're using recursion. When you have recursion, you have two situations: a "base case" (where you don't recurse) and a recursive case. The error you're encountering is incredibly common when the base case isn't solid. I suggest you not just use Yoriz' code, but rather you walk through your own code and then his in order to understand what's happening.

Also, this looks like homework, which often has weird requirements, but I wouldn't use recursion for what you're trying to do there.

Thank you for your reply. I'm still trying to understand some basic python knowledge. Your answer is very helpful.
I move forward a little bit and came up with another weird problem as below.

def ChangeList(itr, K =[0]):
    if itr < 10:
        K.append(1)
        itr += 1
        return ChangeList(K, itr)

    else:

        return K

for i in range(5):
    print (ChangeList(i, K = [0]))
I was trying to using recursion in a for loop, but it did work as I expected. Could you please show me the right way to do it?


RE: RecursionError: maximum recursion depth exceeded in comparison ? - ichabod801 - Mar-15-2019

(Mar-15-2019, 03:03 AM)leoahum Wrote: I was trying to using recursion in a for loop, but it did work as I expected. Could you please show me the right way to do it?

You need to be more clear. We don't know what you expected. What exactly did you expect, and how exactly did your output not match that?


RE: RecursionError: maximum recursion depth exceeded in comparison ? - leoahum - Mar-15-2019

(Mar-15-2019, 04:05 AM)ichabod801 Wrote:
(Mar-15-2019, 03:03 AM)leoahum Wrote: I was trying to using recursion in a for loop, but it did work as I expected. Could you please show me the right way to do it?

You need to be more clear. We don't know what you expected. What exactly did you expect, and how exactly did your output not match that?

def ChangeList(itr, K =[0]):
    if itr < 10:
        K.append(1)
        itr += 1
        return ChangeList(K, itr)
 
    else:
 
        return K
 
for i in range(5):
    print (ChangeList(i, K = [0]))
I was expecting to get 5 lists by execute this script, but it gave an error saying "< not support between instances of list and int"
I thought there would be a series of numbers from 0 to 4 as the parameter "itr".


RE: RecursionError: maximum recursion depth exceeded in comparison ? - ichabod801 - Mar-15-2019

You put your parameters in the wrong order on line 5.


RE: RecursionError: maximum recursion depth exceeded in comparison ? - leoahum - Mar-15-2019

(Mar-15-2019, 01:22 PM)ichabod801 Wrote: You put your parameters in the wrong order on line 5.

Thanks! But in the scripts below. I meet a similar problem. The function works fine by its own, but will have an error in the "for loop". I don't know where goes wrong. The orders are correct. Could you help me? Thanks again!

# This is a script to generates layouts for rectangles. The result is expected to be a list of X,Y values.

# Lx and Ly are the rectangles' X, Y value lists. Different levels represents different categories.

# xgap and ygap are the gaps between rectangles

# H is the list of heights for different categories.

Lx = [[38.297,34.641,25.81,10.33,54.26],[48.99 ,25.82 ,36.51 ,40 ,51.64]]
Ly = [[28.72,25.98,19.36,16.54,41.35],[36.74 ,19.36 ,27.39 ,30 ,38.73]]

xgap = 56
ygap = 19
H = [73.35,111.37]



# itn is the judge for if each rectangle should stay in the same column or start a new column.
# itn2 is the counter.


def MoveAcorHeight(lx, ly, xgap, ygap, H, itn = None, K = [(0,0)], itn2=0):
    #Set a special condition for one element lists.
    if len(lx) == 1:
        return K

    #If the list has more than one elements reset the counter.
    if len(lx) > 1 and itn2 == 0:
        itn = ly[0] + ygap + ly[itn2+1]
        itn2 +=1
        return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)


    #If the judge (itn) > H, start a new column.
    elif 0 < itn2 < len(lx) - 1 and itn > H :
        PrebsX = {}
        for i in K:
            PrebsX[i[0]] = []
        for i in range(len(K)):
            PrebsX[K[i][0]].append(lx[i])

        preJ = []
        for i in PrebsX:
            preJ.append(max(PrebsX[i]))

        J = sum(preJ) + len(preJ) * xgap


        K.append((J, 0))

        PrebsY = {}
        for i in K:
            PrebsY[i[0]] = []
        for i in range(len(K)):
            PrebsY[K[i][0]].append(ly[i])

        preI = PrebsY[K[-1][0]]
        I = sum(preI) + len(preI) * ygap
        itn2 += 1

        itn = I + ly[itn2] + ygap

        return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)

    # To deal with the last element.
    elif itn2 == len(lx) - 1 and itn > H:
        PrebsX = {}
        for i in K:
            PrebsX[i[0]] = []
        for i in range(len(K)):
            PrebsX[K[i][0]].append(lx[i])

        preJ = []
        for i in PrebsX:
            preJ.append(max(PrebsX[i]))

        J = sum(preJ) + len(preJ) * xgap

        K.append((J, 0))
        return K



    # If the judge (itn) <= H, rectangle stay in the same column.
    elif 0< itn2 < len(lx) - 1 and itn <= H:

        PrebsY = {}
        for i in K:
            PrebsY[i[0]] = []
        for i in range(len(K)):
            PrebsY[K[i][0]].append(ly[i])

        preI = PrebsY[K[-1][0]]
        I = sum(preI) + len(preI) * ygap
        K.append((K[itn2 - 1][0], -I))


        itn2 += 1

        itn = I + ly[itn2 - 1] + ygap + ly[itn2]


        return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)

    # To deal with the last element.
    elif itn2 == len(lx) - 1 and itn <= H:
        PrebsY = {}
        for i in K:
            PrebsY[i[0]] = []
        for i in range(len(K)):
            PrebsY[K[i][0]].append(ly[i])

        preI = PrebsY[K[-1][0]]
        I = sum(preI) + len(preI) * ygap
        K.append((K[itn2 - 1][0], -I))

        return K



L = []

### The script works fine when deal with one list, (L.append(MoveAcorHeight(Lx[0], Ly[0], xgap, ygap, H[0]))). But will have a "out of range" warning while running in the for loop.

for i in range(len(Lx)):
    L.append(MoveAcorHeight(Lx[i], Ly[i], xgap, ygap, H[i]))


print (L)



RE: RecursionError: maximum recursion depth exceeded in comparison ? - ichabod801 - Mar-15-2019

Again, you are not being clear. We need the full text of the error you are getting.


RE: RecursionError: maximum recursion depth exceeded in comparison ? - micseydel - Mar-15-2019

I'm taking a guess at your issue... Default function parameters are only evaluated once, at function definition. Take this example:
>>> def f(x=[]): 
...   x.append(True)
...   return x
... 
>>> f()
[True]
>>> f()
[True, True]
>>> f()
[True, True, True]
Here's the idiom for getting around that issue
>>> def g(x=None):
...   if x is None: 
...     x = []
...   x.append(True)
...   return x
... 
>>> g()
[True]
>>> g()
[True]
>>> g()
[True]
Also, an aside, if you can reproduce your problem in 5-10 lines of code (possible here) that tends to be preferable to longer code and makes it more likely that you get an answer which is both as fast as possible and satisfactory.