[英]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.