简体   繁体   English

如何使用初始值递归地将参数输入到此函数中?

[英]How can I recursively input parameters into this function with an initial value?

so I have this function which takes in an array of arrays and outputs an array of arrays with different values (I am attempting genetic programming breeding function).所以我有这个函数,它接收一组数组并输出一组具有不同值的数组(我正在尝试遗传编程育种功能)。

So my problem is as follows: I have a main function which is called evolve(population) which takes in an array of arrays and outputs a new one.所以我的问题如下:我有一个主函数,它被称为evolve(population) ,它接受一组数组并输出一个新的数组。 I would like to keep calling this function on successive populations, however, I'm not quite sure how to?我想继续在连续种群上调用这个函数,但是,我不太确定怎么做?

So we have an initial array x and we pass it into evolve(x) which outputs a new array of arrays y, now I would like to pass evolve(y) into my function and so on for z amount of times (in a for loop).所以我们有一个初始数组 x,我们将它传递给evolve(x) ,它输出一个新的数组 y,现在我想将evolve(y)传递到我的函数中,依此类推 z 次(在 a for环形)。 Can this be done?这能做到吗?

This should do the job quite nicely;这应该可以很好地完成工作;

population = [1, 2, 3, 4]  # any list
z = 10  # evolve 10 times

for i in range(z):
    population = evolve(population)

I think that you should use the recursively callback function :我认为你应该使用递归回调函数

def evolve(population, amount_of_time_you_want_add):
    print(population)

    if amount_of_time_you_want_add > 0:
        # your operation before callback function
        population.append("x")
        return evolve(population, amount_of_time_you_want_add-1)


evolve([1,2,3,4], 5)

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

相关问题 如何防止lambda函数递归调用自身? - How can I prevent a lambda function from calling itself recursively? 如何定义Function参数随输入文件变化? - How Can I Define a Function Whose Parameters Change According to Input File? 如何更改“ id”字段的初始值? - How can I change the initial value of `id` field? 如何找到函数的最佳参数? - How can I find the optimal parameters for a function? 如何将带有参数的 function 包装到带有参数的包装器中? - How can I wrap function with parameters into wrapper with parameters? 如何根据python中的输入更新函数参数? - How do I update function parameters based on input in python? 如何创建具有初始值和“衰减函数”的数组? - How to create array with initial value and 'decay function'? 如果函数返回用户输入的值,如何在不要求用户输入另一个值的情况下在另一个函数中使用该值 - If a function returns a value entered by a user, how can I use that value in another function without asking the user to input another value 我有一个 function 打印输入参数进行计算。 如何返回一个文本字符串,以便它也可以保存为文本文件? - I have a function that prints input parameters for a calculation. How to return a text string so that it can be saved as text file as well? 如何递归拆分人群? - How can I recursively split a population?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM