Python Forum
RecursionError: maximum recursion depth exceeded in comparison ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RecursionError: maximum recursion depth exceeded in comparison ?
#1
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.
Reply
#2
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]
Reply
#3
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.
Reply
#4
(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?
Reply
#5
(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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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".
Reply
#7
You put your parameters in the wrong order on line 5.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(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)
Reply
#9
Again, you are not being clear. We need the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyinstaller Maximum recursion bug scales11 8 11,026 Nov-10-2023, 10:26 PM
Last Post: SuzanneKH09
  Max recursion depth.... Error MeloB 2 1,883 Feb-16-2022, 05:21 PM
Last Post: MeloB
  Time Limit Exceeded error loves 5 3,143 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,747 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Requesting help with my implementation of depth-first search tigerfuchs 6 2,571 Sep-26-2019, 05:47 AM
Last Post: perfringo
  fibonacci ***Time limit exceeded*** frequency 18 10,190 Nov-29-2018, 09:03 PM
Last Post: frequency
  'Time Limit Exceeded' Problem bkpee3 2 5,426 Nov-14-2018, 03:51 AM
Last Post: bkpee3
  Why I get RecursionError on very small amount of data? wavic 3 3,934 Aug-05-2018, 04:55 PM
Last Post: micseydel
  variable loop depth Skaperen 5 4,328 Jul-18-2018, 02:48 AM
Last Post: Skaperen
  maximum recursion depth exceeded saba_keon 3 7,410 Apr-08-2018, 07:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020