Вы можете попробовать это.
>>> a=[[1,2,3],
[4,5,6],
[7,8,9]]
>>>
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> def row_sum(_list,row_idx):
return sum(_list[row_idx])
>>> row_sum(a,0)
6 #summation of 1st row i.e 1+2+3
>>> def col_sum(_list,col_idx):
cols=list(zip(*_list))
return sum(cols[col_idx])
>>> col_sum(a,0)
12 #summation of 1st column i.e 1+4+7
>>> col_sum(a,1)
15 #summation of 2nd column i.e 2+5+8
Вот подход numpy.
import numpy as np
a=[[1,2,3],
[4,5,6],
[7,8,9]]
a=np.array(a)
def nprow_sum(_list,row_idx):
return sum(_list[row_idx])
def npcol_sum(_list,col_idx):
return sum(_list[:,col_idx])