1

I would like to sum from list of list as below

array([[[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]]

What i want is to sum like below

   [1,1,1]+[1,1,1]+[1,1,1]  = 9
   [2,2,2]+[2,2,2]+[2,2,2]  = 18
       ....                 = 27
                            = 36
                            = 45

And return a list like below as the final list:

[9,18,27,36,45]

4 Answers 4

5

You can use np.sum

a = np.array([[[1, 1, 1],
  [2, 2, 2],
  [3, 3, 3],
  [4, 4, 4],
  [5, 5, 5]],

 [[1, 1, 1],
  [2, 2, 2],
  [3, 3, 3],
  [4, 4, 4],
  [5, 5, 5]],

 [[1, 1, 1],
 [2, 2, 2],
 [3, 3, 3],
 [4, 4, 4],
 [5, 5, 5]]])

res = np.sum(a, axis=(0,2))
# Does reduction along axis 0 and 2 by doing summation.
# numpy takes tuple of axis indices to do reduction 
# simultaneously along those axis.
print(res.tolist())
>> [ 9, 18, 27, 36, 45]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks , can you explain more on axis (0,2) please
@foy Summation will be done along axis 0 and 2.
1
import numpy as np
lis=np.array([[[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]])
print(lis.sum(axis=0).sum(axis=1))
easiest I think with numpy.
output 

[ 9 18 27 36 45]

Comments

0

Using zip()

Code:

import numpy as np
lis=np.array([[[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]])
res=[]

for i in zip(*lis):
    res.append(sum(sum(i)))
print(res)

Output:

[9, 18, 27, 36, 45]

List comprehension:

print([sum(sum(i)) for i in zip(*lis)]) #Same output.

Comments

-1
a = [[[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
      [2, 2, 2],
      [3, 3, 3],
      [4, 4, 4],
      [5, 5, 5]],

     [[1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]]

result = [sum(x)+sum(y)+sum(z) for x,y,z in zip(a[0], a[1], a[1])]

2 Comments

Welcome to stackoverflow and thank you for answering your first question. Make sure to add a description of what your code does and how it answers the question!
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.