希望将函数迭代10次,并取其值的平均值(python)
我有一个有4个参数的函数,我想迭代这个函数10次,并且把这个计数器作为函数的参数。下面是我要迭代的函数:希望将函数迭代10次,并取其值的平均值(python),python,function,loops,Python,Function,Loops,我有一个有4个参数的函数,我想迭代这个函数10次,并且把这个计数器作为函数的参数。下面是我要迭代的函数: np.random.seed(1) def propagate ( seed , network , threshold , steps ): "" "Start cascade from node` seed` of network `net`." "" activated = [ se
np.random.seed(1)
def propagate ( seed , network , threshold , steps ):
"" "Start cascade from node` seed` of network `net`." ""
activated = [ seed ]
pocket = [ seed ]
exposition = {}
time = 0
while time < steps :
time += 1
# propagate
for seed in pocket :
for out_node in network . successors( seed ):
add_weight = network [ seed ] [ out_node ] [ "weight" ]
if out_node not in activated :
if out_node in exposition :
exposition [ out_node ] += add_weight
else :
exposition [ out_node ] = add_weight
# activate
pocket = []
for node , total in exposition .items ():
if total = threshold :
pocket . append ( node )
activated += pocket [:]
for node in pocket :
del exposition [ node ]
return len ( activated )z
您应该附加函数的结果:
act_nodes=[]
for i in range(1,5):
x=propagate(1,B,5,i)
act_nodes.append(x)
“activated”在函数内部声明,而不是在函数外部,这是的错误。
另外,为什么在范围(1,5)内循环?这将运行4次,但你说需要10次?哦,该死!你能告诉我你做了什么吗?它将输出存储在x中并添加到列表中?@Chai是的,没错
act_nodes=[]
for i in range(1,5):
x=propagate(1,B,5,i)
act_nodes.append(x)