I want to calculate the row sum and column sum of a matrix in python; however, because of infosec requirements I cannot use any external libraries. So to create a matrix, I've used a list of lists, as follows:
matrix = [[0 for x in range(5)] for y in range(5)]
for pos in range(5):
matrix[pos][pos]=1
matrix[2][2]= 0
Now what I want to do is perform a rowsum and a column sum of the matrix. I know how to do a row sum, that's quite easy:
sum(matrix[0])
but what if I wanted to do a column sum? Is there a more elegant and pythonic way to accomplish that beyond brute-forcing it with a for loop, a la
sumval = 0
for pos in range(len(matrix[0])):
sumval = matrix[pos][0] + sumval
which would work, but it isn't pythonic at all.
Can anyone help me out?