I have a Python array, like so:
[[1,2,3], [1,2,3]]
I can add the row by doing sum(array[i]), how can I sum a column, using a
sum(array[i])
Using a for loop (in a generator expression):
for
data = [[1,2,3], [1,2,3]] column = 1 print(sum(row[column] for row in data)) # -> 4
You can use numpy:
import numpy as np a = np.array([[1,2,3],[1,2,3]]) a.sum(0)