-1

I want to find a sum of a column in a matrix without using any package.

a = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]

If I want to extract a row I can write a[i] for ith row, but how to extract a particular column?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
anadi
  • 63
  • 1
  • 6

2 Answers2

0

So you can get the ith row with a[i] and you can get the nth value from row i with a[i][n] now if you want the column as a whole you will want something like this:

def getColumn(data,col):
    return [k[col] for k in data]
kpie
  • 9,588
  • 5
  • 28
  • 50
0

You can access any element :

def sumColumn(m, column):
    sum = 0
    for row in range(len(m)):
        sum += m[row][column]
    return sum

column = 1
print("The sum for column number", column, "is", sumColumn(matrix, column))
dper
  • 884
  • 1
  • 8
  • 31