Blaise Delaney
Blaise Delaney

Reputation: 321

Sum element by element list of lists python

I am experiencing some issues with the following task:

I have a list of lists.

I have called "weighted_prob" the list of lists, where each element of "weighted_prob" is called "weighted_variable".

There are 32 elements in weighted_prob, each one being a list of 100 floats.

This is what it looks like:

 for i in arange(0,2):
      print weighted_prob[i]

outcome:
   [  1.56147373e-02   2.04783441e-03   2.60245622e-03   2.34647692e-03
   1.96250797e-03   2.60245622e-03   1.45054937e-03   1.74919189e-03
   2.00517119e-03   2.04783441e-03   1.62120224e-03   1.87718154e-03
   1.96250797e-03   2.34647692e-03   2.00517119e-03   1.57853902e-03
   1.74919189e-03   2.21848727e-03   1.53587580e-03   1.36522294e-03
   1.19457007e-03   1.32255972e-03   1.10924364e-03   9.38590770e-04
   7.25274686e-04   8.95927553e-04   6.39948252e-04   9.81253986e-04
   8.53264336e-04   3.41305734e-04   5.97285035e-04   5.11958602e-04
   6.39948252e-04   5.11958602e-04   4.69295385e-04   2.98642518e-04
   2.13316084e-04   2.13316084e-04   3.41305734e-04   3.83968951e-04
   2.98642518e-04   1.70652867e-04   2.98642518e-04   1.27989650e-04
   1.70652867e-04   1.27989650e-04   2.55979301e-04   1.27989650e-04
   8.53264336e-05   1.27989650e-04   4.26632168e-05   1.27989650e-04
   4.26632168e-05   4.26632168e-05   0.00000000e+00   0.00000000e+00
   1.27989650e-04   0.00000000e+00   0.00000000e+00   0.00000000e+00
   4.26632168e-05   0.00000000e+00   4.26632168e-05   0.00000000e+00
   0.00000000e+00   0.00000000e+00   4.26632168e-05   4.26632168e-05
   0.00000000e+00   4.26632168e-05   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00]  
 [  1.53587591e-02   1.87718167e-03   2.17582421e-03   2.13316099e-03
   2.77310929e-03   2.47446675e-03   2.34647709e-03   2.00517133e-03
   1.96250811e-03   2.17582421e-03   2.73044607e-03   2.68778285e-03
   1.74919201e-03   2.34647709e-03   2.85843573e-03   1.91984489e-03
   2.38914031e-03   2.64511963e-03   1.79185523e-03   1.91984489e-03
   1.74919201e-03   1.32255981e-03   1.66386557e-03   1.57853913e-03
   8.10601177e-04   1.27989659e-03   8.95927616e-04   8.95927616e-04
   8.53264397e-04   8.53264397e-04   5.97285078e-04   6.39948297e-04
   6.39948297e-04   6.39948297e-04   5.97285078e-04   4.26632198e-04
   5.54621858e-04   3.83968978e-04   3.41305759e-04   1.70652879e-04
   4.26632198e-04   1.70652879e-04   2.55979319e-04   5.11958638e-04
   2.98642539e-04   8.53264397e-05   2.55979319e-04   8.53264397e-05
   1.70652879e-04   0.00000000e+00   4.26632198e-05   8.53264397e-05
   4.26632198e-05   1.27989659e-04   4.26632198e-05   4.26632198e-05
   4.26632198e-05   8.53264397e-05   8.53264397e-05   4.26632198e-05
   4.26632198e-05   4.26632198e-05   4.26632198e-05   0.00000000e+00
   4.26632198e-05   4.26632198e-05   4.26632198e-05   0.00000000e+00
   0.00000000e+00   4.26632198e-05   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
   0.00000000e+00   0.00000000e+00   0.00000000e+00]

I'd like to add up all the ith elements of each weighted_variable.

In other words, I wish to take say, the zeroth element of each list in weighted_prob (therefore obtaining 32 variable), get the sum of the variables, store this sum in a array. This must be applied to all of the 100 elements of each weighted_variable, for all the 32 "weighted_variables".

Here is my attempt:

 add_prob = []    

 for weighted_variable in weighted_prob:
     for i in range(len(weighted_variable)-1):
         sum_per_bin = []
         sum_per_bin.append(partitioning[i])
          dummy = sum(sum_per_bin)

     add_prob.append(dummy)

but the command

 print add_prob 

gives a list of 32 zeroes, and I am positive that's wrong.

Can anyone help me out, please? For instance, maybe, there is a better way to do this, without a nested for loop?

Many Thanks

Blaise

Upvotes: 2

Views: 2896

Answers (2)

YXD
YXD

Reputation: 32511

Are you using Numpy?

If you are, I think you simply want np.sum(weighted_prob, axis=0)

Upvotes: 4

zhangyangyu
zhangyangyu

Reputation: 8610

sum_per_bin = []

This statement in the nested for assign a new list to the variable every time. So your dummy will only be the last item.

To get what you want:

[sum(x) for x in zip(*weighted_prob)]

Upvotes: 3

Related Questions