Skip to content
Related Articles

Related Articles

Python Program to Sort Matrix by Sliced Row and Column Summation

View Discussion
Improve Article
Save Article
  • Last Updated : 18 Jul, 2021
View Discussion
Improve Article
Save Article

Given a Matrix and a range of indices, the task is to write a python program that can sort a matrix on the basis of the sum of only given range of indices of each row and column i.e. the rows and columns are to sliced from a given start to end index, further, matrix are sorted using only those slices sum from each row or column.

Input : test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]], i, j = 1, 3 
Output : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]] 
Explanation : 7 < 8 < 9 < 10, is summation of 1st and 2nd element.
 

Input : test_list = [[1, 4, 3, 1, 3], [23, 5, 5, 3], [2, 3, 5, 1, 6]], i, j = 1, 3 
Output : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [23, 5, 5, 3]] 
Explanation : 7 < 8 < 10, is summation of 1st and 2nd element. 
 

Summation on rows

Method 1 : Using sort(), slice and sum()

In this, we perform the task of in-place sorting using sort(), and summation is done using sum(), slice operation is done using list slicing, which together forms as key function for sorting.

Example:

Python3




# get sliced summation
def get_sliced_sum(row):
    return sum(row[i:j])
  
  
# initializing list
test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4],
             [23, 5, 5, 3], [2, 3, 5, 1, 6]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing range
i, j = 1, 3
  
# performing sort
test_list.sort(key=get_sliced_sum)
  
# printing result
print("Sorted List : " + str(test_list))

Output:

The original list is : [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]]

Sorted List : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]]

Method 2 : Using sorted(), lambda, sum() and slicing 

In this, the task of performing sort is done using sorted() and lambda function is used to get a summation of sliced rendered using one-statement without external function call.

Example:

Python3




# initializing list
test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4],
             [23, 5, 5, 3], [2, 3, 5, 1, 6]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing range
i, j = 1, 3
  
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
  
# printing result
print("Sorted List : " + str(res))

Output:

The original list is : [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]]

Sorted List : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]]

Summation on column

Method 1 : Using sort(), slice and sum()

In this, we perform transpose of a base matrix and then perform the usual task of getting slice using above method 1, after sorting, the matrix is again converted to its transposed format. 

Example:

Python3




# get sliced summation
def get_sliced_sum(row):
    return sum(row[i:j])
  
  
# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2],
             [23, 5, 5, 3], [2, 3, 5, 1]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing range
i, j = 1, 3
  
# transposing matrix
test_list = list(zip(*test_list))
  
# performing sort
test_list.sort(key=get_sliced_sum)
  
# performing transpose again to get 
# result.
test_list = zip(*test_list)
  
# converting list of tuples to list of 
# lists
res = [list(sub) for sub in test_list]
  
# printing result
print("Sorted List Columnwise : " + str(res))

Output:

The original list is : [[1, 4, 3, 1], [3, 4, 5, 2], [23, 5, 5, 3], [2, 3, 5, 1]]

Sorted List Columnwise : [[1, 4, 3, 1], [2, 4, 5, 3], [3, 5, 5, 23], [1, 3, 5, 2]]

Method 2 : Using sorted(), lambda, sum() and slicing 

In this, we perform transpose of a base matrix and then perform the usual task of getting slice using above method 2, after sorting, the matrix is again converted to its transposed format. 

Example:

Python3




# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2], 
             [23, 5, 5, 3], [2, 3, 5, 1]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing range
i, j = 1, 3
  
# transposing matrix
test_list = zip(*test_list)
  
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
  
# performing transpose again to get result.
res = zip(*res)
  
# converting list of tuples to list of lists
res = [list(sub) for sub in res]
  
# printing result
print("Sorted List Columnwise : " + str(list(res)))

Output:

The original list is : [[1, 4, 3, 1], [3, 4, 5, 2], [23, 5, 5, 3], [2, 3, 5, 1]]

Sorted List Columnwise : [[1, 4, 3, 1], [2, 4, 5, 3], [3, 5, 5, 23], [1, 3, 5, 2]]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!