繁体   English   中英

如何通过将上次运行 function 的 output 作为输入来重复 function

[英]How do I repeat a function by putting the output from the previous time I ran the function as the input

k = np.log(2)/tSmall
N24Small = N0Small * math.exp(k*t)
k = np.log(2)/tBig
N24Big = N0Big * math.exp(k*t)
    SizeSmall = np.random.normal(meanSmall, sdSmall, int(N24Small)) 
SizeBig = np.random.normal(meanBig, sdBig, int(N24Big))
bigspeed = vcalc(SizeBig)
allSpeed = np.concatenate((smallspeed, bigspeed))
allSpeed.tolist()
allSpeed_hun = (allSpeed[0:99])
allSpeed_hun.tolist()
num_smallspeed = len(set(allSpeed_hun).intersection(smallspeed))
num_bigspeed = len(set(allSpeed_hun).intersection(bigspeed))

我有这个 function 我想从最后两个变量中获取数据并将其用作变量 N0small 和 N0 big

您所描述的是递归 function。 我建议你谷歌“python递归函数”来阅读详细信息,但最终结果看起来像这样:

def my_recursive_function(N0Small, N0Big):
    k = np.log(2)/tSmall
    N24Small = N0Small * math.exp(k*t)
    k = np.log(2)/tBig
    N24Big = N0Big * math.exp(k*t)
        SizeSmall = np.random.normal(meanSmall, sdSmall, int(N24Small)) 
    SizeBig = np.random.normal(meanBig, sdBig, int(N24Big))
    bigspeed = vcalc(SizeBig)
    allSpeed = np.concatenate((smallspeed, bigspeed))
    allSpeed.tolist()
    allSpeed_hun = (allSpeed[0:99])
    allSpeed_hun.tolist()
    num_smallspeed = len(set(allSpeed_hun).intersection(smallspeed))
    num_bigspeed = len(set(allSpeed_hun).intersection(bigspeed))
    
    # exit condition
    if (exit_condition):
        # i am taking a guess here
        return num_smallspeed, num_bigspeed
    return my_recursive_function(num_smallspeed, num_bigspeed)

您将需要一个退出条件,以便 function 可以停止调用自身。 如果您正在做的计算收敛到某个值,也许您可以比较 N0Small 和 num_smallspeed 并在它们之间的差异足够小时退出? 我只是在这里猜测,因为我不知道这段代码实际上在做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM