+ 1

This is my 2d matrix...how to print sum of each column using list comprehension

mat=[ [1,2,3] , [4,5,6] ]

20th Oct 2018, 1:31 PM
Pranay Bhoir.
Pranay Bhoir. - avatar
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]
20th Oct 2018, 1:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar