Sum of diagonal elements in a matrix

后端 未结 11 1938
轻奢々
轻奢々 2021-02-12 11:42

I am trying to find out the sum of the diagonal elements in a matrix. Here, n is the size of the square matrix and a is the matrix. Can someone explain this to me what is happen

11条回答
  •  无人及你
    2021-02-12 12:06

    def sum_up_diagonals(li):
    index = len(li)
    first_dia =  sum(li[i][i]for i in range(index))
    second_dia = sum(li[i][index-i-1]for i in range(index))
    return (first_dia,second_dia)
    

    Pass in your list. This should work for you :)

提交回复
热议问题