0

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?

Brad Davis
  • 1,063
  • 3
  • 18
  • 38
  • You would probably do well to look into numpy and/or pandas. They're designed for working with matrices. – jpmc26 Dec 14 '18 at 19:59
  • As indicated in the post, "because of infosec requirements I cannot use any external libraries", i.e. I cannot use numpy or pandas. I can only basic python 2 or 3 installations. These are run on highly secured servers that do not have read or write access to the internet. – Brad Davis Dec 14 '18 at 23:20
  • Maybe worth pushing for getting them evaluated and approved and pre-installed, then, even if you can't for this project. These are staples of Python usage. – jpmc26 Dec 15 '18 at 00:39
  • Yes, I know they are a staple of Python, so does my company - Amazon. Lots of the servers are allowed to have Numpy, and Scipy, and Pandas, etc, installed. Some of them come with these packages pre-configured. Lots of them allow you to install packages using git. Those rules do not apply on the machines I'm working on. – Brad Davis Dec 15 '18 at 21:14

5 Answers5

5
colsum = sum(row[0] for row in matrix)

As a note for others who look at this question though, this really is a task best left to numpy. OP is not allowed external libraries however.

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
2

I'd suggest:

s = 0
for row in matrix:
    s += row[0]

which is the same as you are doing but a bit more readable.

Using something like:

s = sum([row[0] for row in matrix])

is also readable, but slower because you need to do one pass to collect the row[0] elements, and a second to sum.

Mccosh
  • 33
  • 3
  • Thanks for your explanation about why the first solution you proposed would work faster than the second (also proposed by Paritosh Singh). I am going with the second option because the matricies I will be using are so small the performance difference doesn't amount to anything, so it'll make the code neater and more compact. But that's useful to know! – Brad Davis Dec 14 '18 at 19:37
1

You can use:

sum([matrix[i][0] for i in range(len(matrix[0]))])
0

I can suggest, define a method to calculate the sum by rows, which returns the list of sums:

def sum_rows(matrix):
  return [sum(row) for row in matrix]

Then define a method that calls sum_rows(matrix) on the transposed matrix:

def sum_cols(matrix):
  return sum_rows(map(list, zip(*matrix)))

For transposing a matrix: Transpose list of lists

Alternative to transpose:

def sum_cols_alt(matrix):
  return [ sum(row[i] for row in matrix) for i, _ in enumerate(matrix) ]
iGian
  • 11,023
  • 3
  • 21
  • 36
0

One way to do this is to use the map function:

for sum_row, sum_col in zip(map(sum, matrix), map(sum, zip(*matrix))):
    print(sum_row, sum_col)