How to sort matrix based on sum of rows in Python

1 Answer

0 votes
def get_sum(row):
   return sum(row)

 
matrix = [[1,   2,   3,   4,  0],
          [5,   6, 100,   8,  1],
          [2, 100,   8, 100,  3],
          [1,   7, 100,   9,  6],
          [9,  10,  11,  12, 13]]


matrix.sort(key = get_sum)

print(matrix)


'''
run:

[[1, 2, 3, 4, 0], [9, 10, 11, 12, 13], [5, 6, 100, 8, 1], [1, 7, 100, 9, 6], [2, 100, 8, 100, 3]]

'''


 



answered Jun 22, 2023 by avibootz
...