Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算while循环内的平均值_Python_Pandas_Loops_While Loop_Mean - Fatal编程技术网

Python 计算while循环内的平均值

Python 计算while循环内的平均值,python,pandas,loops,while-loop,mean,Python,Pandas,Loops,While Loop,Mean,所以我有这个代码: def run_simulation(nba = 100, maxIter = 10, probI = 0.2, probR = 0.2, probI_init = 0.1, network = nx.erdos_renyi_graph, netParam=0.1, rep=10): r = 0 while r < rep: init(nba, probI) i = 0 while i < maxIter: step(pro

所以我有这个代码:

def run_simulation(nba = 100, maxIter = 10, probI = 0.2, probR = 0.2, probI_init = 0.1, network = nx.erdos_renyi_graph, netParam=0.1, rep=10):
r = 0
while r < rep:
    init(nba, probI)
    i = 0
    while i < maxIter:
        step(probR, probI)
        nbI = collect_statistics()
        if nbI == 0:
            break
        i = i + 1
    print(statS[-1])
    r = r +1

run_simulation()
def run_模拟(nba=100,maxIter=10,probI=0.2,probR=0.2,probI_init=0.1,network=nx.erdos_renyi_图,netParam=0.1,rep=10):
r=0
而r
此代码生成如下10个数字:

我的问题是:有没有办法计算这10个数字的平均值,以及如何计算


提前多谢

因为
statS
显然是在这个例程之外创建和修改的一个全局列表,所以有点难说,但显而易见的答案是在循环退出后打印(sum(statS)/len(statS))

hi yes statS是一个列表。每次运行模拟时,我需要计算此列表中最后一个数字的平均值,这是屏幕截图上模拟中出现的10个数字。所以我不得不写:print(sum(statS[-1])/len(statS)),但是,它显示了这个错误:“TypeError:‘int’object不可编辑”您不能获取单个值的和。把整个列表都放进去。“列表最后一个数字的平均值”没有任何意义。一个数字没有平均值,这就是它告诉你的。我假设您真的想打印最后一个数字,然后打印到目前为止所有数字的平均值。我在回答中的那句话正好可以做到这一点。