0

Lets say I have the following array:

My_Array = [[1,2,3],[4,5,6],[7,8,9]]

I want to sum each of these arrays so the output would be

My_Array = [[6],[15],[24]]

How can I do this without using Numpy?

3 Answers 3

2

Python provide an inbuilt function sum() which sums up the numbers in the list.

My_Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print([sum(x) for x in My_Array])

If you want to get sum value also as a list then

[[sum(x)] for x in My_Array]
Sign up to request clarification or add additional context in comments.

Comments

0

A simple solution would be:

[[sum(i)] for i in My_Array]

I recommend taking a look at Python's naming convention. My_Array isn't quite pythonic.

Comments

0

You could use for loop to go over array and sum each array individually and then store it again inside the array.

for i in range(len(My_Array)):
    My_Array[i]=[sum(My_Array[i])]

Comments

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.