Python:计算一大堆数组的平均值而不保存它们

时间:2017-02-27 19:40:12

标签: python arrays numpy average

我有大量的列表,我想计算平均值而不存储所有列表。有没有python实现呢?

由于

1 个答案:

答案 0 :(得分:1)

如果你想平均整个数组(必须是兼容的形状):

running_sum = None
count = 0
for a in yourarraysource:
    if running_sum is None:
        running_sum = a
    else:
        running_sum = running_sum + a
    count += 1
average = running_sum / count

如果你想平均所有元素:

running_sum = 0
count = 0
for a in yourarraysource:
    running_sum += a.sum()
    count += a.size
average = running_sum / count