Bull's-eye 2020-04-28 01:14 采纳率: 0%
浏览 192

python(numpy)中,如何正确使用递归语句?

我定义了一个根据原向量生成新向量的函数,然后也定义了一个禁忌表,我想让每次新生成的向量都不属于禁忌表中,否则就要重新去生成新的向量。

下面代码就是我定义的函数:

    import numpy as np


    def create_new_x(t, x):
        if 0 <= t < 30:
            s = []
            while len(s) < 6:
                a = np.random.randint(0, 8)
                if a not in s:
                    s.append(a)
            x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
            x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
            x[s[4]], x[s[5]] = x[s[5]], x[s[4]]
            x_new = x
            if x in tabu_list:
                create_new_x(t, x)
        elif 30 <= t < 70:
            s = []
            while len(s) < 4:
                a = np.random.randint(0, 8)
                if a not in s:
                    s.append(a)
            x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
            x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
            x_new = x
            if x in tabu_list:
                create_new_x(t, x)
        else:
            s = []
            while len(s) < 4:
                a = np.random.randint(0, 8)
                if a not in s:
                    s.append(a)
            x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
            x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
            x_new = x
            if x in tabu_list:
                create_new_x(t, x)
        return x_new

其中,t只是一个自然增长的数,根据迭代次数来变化,x是一向量,比如x = [1,2,3,4,5,6,7,8],“tabu_list”就是我定义的禁忌表(一个20行8列的矩阵,每一行也是类似于x的向量,每行中的各个元素互不相同),然后执行的时候,就会报错,报错的最后一行如下。

RecursionError: maximum recursion depth exceeded while calling a Python object。

然而我用dubug时,好像又不会报错,请问各位大佬,这种情况是因为什么造成的,是我的递归写的不对还是因为其他的?

  • 写回答

1条回答 默认 最新

  • 放风喽 2020-04-28 09:25
    关注

    http://blog.51cto.com/leyex/1884041
    随手百度一下就能找到

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记