Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
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]
Add a comment
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.
My_Array
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])]
Required, but never shown
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.
Explore related questions
See similar questions with these tags.