Python中数组的递归问题

Python中数组的递归问题,python,python-3.x,recursion,Python,Python 3.x,Recursion,我正在开发一个递归函数。这段代码目前速度很快,但在优化它之前,我面临一个问题 一旦递归函数调用结束(我的意思是我的算法正在倒转),case\u courante变量将从堆栈中弹出到上一个值,但对于数组dernier\u match和tour,情况并非如此。我不明白为什么 这是我的密码: #!/usr/bin/python temps_min=21 temps_max=45 nb_time_slot=245 categorie=[[6,6,6,4,4,2,2,99],[6,6,6,4,4,2,2

我正在开发一个递归函数。这段代码目前速度很快,但在优化它之前,我面临一个问题

一旦递归函数调用结束(我的意思是我的算法正在倒转),
case\u courante
变量将从堆栈中弹出到上一个值,但对于数组
dernier\u match
tour
,情况并非如此。我不明白为什么

这是我的密码:

#!/usr/bin/python
temps_min=21
temps_max=45
nb_time_slot=245

categorie=[[6,6,6,4,4,2,2,99],[6,6,6,4,4,2,2,99],[6,6,6,4,4,2,2,99],[3,3,3,2,2,2,99],[3,3,3,2,2,2,99],[4,4,4,4,2,2,99],[6,6,6,2,2,2,99],[6,6,6,2,2,2,99],[6,6,6,2,2,2,99],[6,6,6,2,2,2,99],[1,1,1,1,1,1,1,1,1,1,1,1,1]]
dernier_match_depart=[0]*10
case_courante_depart=0
tour_depart=[0]*10
echeancier =[None]*(nb_time_slot+10)
profondeur =0

#function
def choix(prof, case_courante, dernier_match, tour):
        global categorie,temps_min,temps_max,nb_time_slot,echeancier
        for i in range (0,10):
                print ("Profondeur:", prof)
                print(i)
                if (dernier_match[i] == 0):
                        for x in range (case_courante,case_courante + categorie[i][tour[i]]):
                                echeancier[x] = i
                        case_courante = case_courante + categorie[i][tour[i]]
                        dernier_match[i] = case_courante
                        tour[i] = tour[i] + 1
                        print echeancier
                        choix(prof+1, case_courante, dernier_match, tour)
                print ("case courante:", case_courante)
                print ("tour", tour)
                print ("dernier_match",dernier_match)
                if (categorie[i][tour[i]] != 99 and dernier_match[i]+temps_min < case_courante and dernier_match[i]+temps_max > case_courante and case_courante<nb_time_slot):
                        print ("if principal\n")
                        print ("slots dans ce tour",categorie[i][tour[i]])
                        for x in range (case_courante,case_courante + categorie[i][tour[i]]):
                                echeancier[x] = i
                        case_courante = case_courante + categorie[i][tour[i]]
                        dernier_match[i] = case_courante
                        tour[i] = tour[i] + 1
                        print echeancier
                        choix(prof+1, case_courante, dernier_match, tour)
                for a in range (0,9):
                        if (categorie[a][tour[a]] != 99):
                                break
                        else:
                                if (a == 9):
                                        print ("Solution trouvee\n")
                                        print (echeancier)
                                        exit()
#main
choix(0,case_courante_depart,dernier_match_depart, tour_depart)
#/usr/bin/python
temps_min=21
最大温度=45
nb_时间槽=245
分类=[[6,6,6,4,4,2,2,99]、[6,6,4,4,2,2,99]、[6,6,6,4,2,2,99]、[3,3,2,2,2,99]、[3,3,3,2,2,2,99]、[4,4,4,2,2,99]、[6,6,2,2,2,2,2,2,99]、[6,6,6,2,2,2,2,2,2,2,2,2,2,2,1,1,1]、[6,6,6,6,6,6,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,
dernier_匹配_离开=[0]*10
案例\u courante\u depart=0
旅游出发=[0]*10
echeancier=[None]*(nb\U时间槽+10)
profondeur=0
#作用
def choix(教授、case_courante、dernier_match、巡回赛):
全局分类、最小时间段、最大时间段、nb时间段、echeancier
对于范围(0,10)内的i:
打印(“Profondeur:,prof”)
印刷品(一)
如果(dernier_匹配[i]==0):
对于范围内的x(case_courante,case_courante+分类[i][tour[i]]):
echeancier[x]=i
case\u courante=case\u courante+categorie[i][tour[i]]
dernier_match[i]=案例库兰特
巡更[i]=巡更[i]+1
印刷机
choix(教授+1,凯斯·库兰特,德尼尔·赛程,巡回赛)
打印(“案例库兰特:”,案例库兰特)
打印(“巡回”,巡回)
打印(“dernier_匹配”,dernier_匹配)

如果(分类[i][tour[i]!=99和dernier_匹配[i]+temps_mincase_courante和case_courante这是因为您重新分配了
case_courante

case_courante = case_courante + categorie[i][tour[i]]
但您仅修改
巡更
德尼尔匹配
的元素:

dernier_match[i] = case_courante
tour[i] = tour[i] + 1
因此,
case\u courante
不断引用不同的不可变整数,但其他人总是引用其原始的
列表
s,从不引用任何其他内容

更新:

您的递归函数似乎有两个递归调用站点(都相同):

我最初的猜测(因为我不知道所需的功能)是传递列表的副本:

choix(prof+1, case_courante, dernier_match[:], tour[:])

你的问题从来没有确定什么是正确的。你是想返回变量的以前状态,如
case\u courante
现在所做的,还是想保留当前状态,如
tour
dernier\u match
现在所做的?很抱歉,不清楚:我想返回变量的以前状态(就像在做
案例\u courante
choix(prof+1, case_courante, dernier_match[:], tour[:])