迭代列表或数组时的内存问题

时间:2016-03-02 01:08:18

标签: python arrays numpy memory

我有一个数组列表,以便:

arr = [array([1,2,3,4,5]), array([1,4,6,7]) .......] 

包含40000个数组。理想情况下,我希望将它作为2d numpy数组,但我无法保证所有数组的长度都相同。

我想对列表中的所有值执行基本操作,例如:

out = (3*arr)+2

但是我很快耗尽了内存(在32GB机器上),所以它显然非常低效。我已经尝试迭代列表并将结果附加到新列表,但这同样效率低下。

有没有一种有效的方法来实现这一目标?

---------------编辑--------------

arr看起来像:

[array([ 451.481649,  456.490319,  461.498989,  466.507659,  471.516329,
    476.524999,  481.533669,  486.542339,  491.551009,  496.559679,
    501.568349,  506.577019,  511.585689,  516.594359,  521.603029,
    526.611699,  531.62037 ,  536.62904 ,  541.63771 ,  546.64638 ,
    551.65505 ,  556.66372 ,  561.67239 ,  566.68106 ,  571.68973 ,
    576.6984  ,  581.70707 ,  586.71574 ,  591.72441 ,  596.73308 ,
    601.74175 ,  606.75042 ,  611.75909 ,  616.76776 ,  621.77643 ,
    626.7851  ,  631.79377 ,  636.80244 ,  641.811111,  646.819781,
    651.828451,  656.837121,  661.845791,  666.854461,  671.863131]),
array([ 451.481649,  456.490319,  461.498989,  466.507659,  471.516329,
    476.524999,  481.533669,  486.542339,  491.551009,  496.559679,
    501.568349,  506.577019,  511.585689,  516.594359,  521.603029,
    526.611699,  531.62037 ,  536.62904 ,  541.63771 ,  546.64638 ,
    551.65505 ,  556.66372 ,  561.67239 ,  566.68106 ,  571.68973 ,
    576.6984  ,  581.70707 ,  586.71574 ,  591.72441 ,  596.73308 ,
    601.74175 ,  606.75042 ,  611.75909 ,  616.76776 ,  621.77643 ,
    626.7851  ,  631.79377 ,  636.80244 ,  641.811111,  646.819781,
    651.828451,  656.837121,  661.845791,  666.854461,  671.863131]),
array([ 451.481649,  456.490319,  461.498989,  466.507659,  471.516329,
    476.524999,  481.533669,  486.542339,  491.551009,  496.559679,
    501.568349,  506.577019,  511.585689,  516.594359,  521.603029,
    526.611699,  531.62037 ,  536.62904 ,  541.63771 ,  546.64638 ,
    551.65505 ,  556.66372 ,  561.67239 ,  566.68106 ,  571.68973 ,
    576.6984  ,  581.70707 ,  586.71574 ,  591.72441 ,  596.73308 ,
    601.74175 ,  606.75042 ,  611.75909 ,  616.76776 ,  621.77643 ,
    626.7851  ,  631.79377 ,  636.80244 ,  641.811111,  646.819781,
    651.828451,  656.837121,  661.845791,  666.854461,  671.863131])]

1 个答案:

答案 0 :(得分:2)

如果你真的像你写的那样做,很可能你会遇到内存问题,因为

out = (3*arr)+2

将您的数组复制3次,然后尝试追加它。数组越大,常数越大,内存爆炸就越大。

要在没有内存问题的情况下实现您的目标,请使用

out = [[3*x + 2 for x in arr_list] for arr_list in arr]