+ 1
This is my 2d matrix...how to print sum of each column using list comprehension
mat=[ [1,2,3] , [4,5,6] ]
2 Respuestas
+ 10
mat = [[1,2,3],[4,5,6]]
print(list(sum(row[col] for row in mat) for col in range(len(mat[0]))))
>>>
[5, 7, 9]
Of course you can transform it into a numpy array and execute real array operations:
import numpy as np
print(np.sum(mat, axis=0))
>>>
[5 7 9]
+ 2
Here how use list comprehension https://www.pythonforbeginners.com/basics/list-comprehensions-in-JUMP_LINK__&&__python__&&__JUMP_LINK