# Python Program to Sort Matrix by Sliced Row and Column Summation

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]]

Time Complexity: O(nlogn+mlogm)
Auxiliary Space: O(1)

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]]

The time complexity of this approach is O(nlogn), where n is the number of sublists in the input list.
The auxiliary space complexity of this approach is O(n), where n is the number of sublists in the input list.

Method 3:  Using the built-in map() and lambda functions

Step-by-step approach:

• Define a nested list test_list with 4 sublists each containing 4 elements.
• Print the original list test_list.
• Define two variables i and j with values 1 and 3 respectively.
• Use map() and lambda to extract sublists from test_list using slice indices i and j, then compute the sum of each sublist.
• Convert the resulting map object into a list of sums using the list() function.
• Use the zip() function to combine each sum with its corresponding sublist from test_list.
• Sort the resulting list of tuples by the sums using sorted() function and a lambda function to specify the sorting key.
• Use a list comprehension to extract the sorted sublists from the sorted list of tuples.
• Print the sorted list of sublists res.

Below is the implementation of the above approach:

## 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` `# extracting sublists and computing sums using map() and lambda``sums ``=` `list``(``map``(``lambda` `x: ``sum``(x[i:j]), test_list))` `# sorting original list by computed sums``res ``=` `[x ``for` `_, x ``in` `sorted``(``zip``(sums, 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, 3, 5, 1], [3, 4, 5, 2], [23, 5, 5, 3]]```

Time complexity: O(n log n) – sorting the list takes O(n log n) time
Auxiliary space: O(n) – we use a list to store the computed sums

### 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]]

Time Complexity: O(nlogn+mlogm)
Auxiliary Space: O(k)

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]]

Method 3: using list comprehension and lambda function. Here are the steps for this approach:

Step-by-step approach:

1. Transpose the given list of lists using zip(*test_list).
2. Use list comprehension to create a list of tuples where each tuple contains a column from the transposed list and the sliced summation of that column.
3. Sort the list of tuples based on the sliced summation using the lambda function.
4. Transpose the sorted list of tuples using zip(*sorted_list).
5. Convert the list of tuples to a list of lists.

Below is the implementation of the above approach:

## Python3

 `# get sliced summation``def` `get_sliced_sum(row, i, j):``    ``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_transposed ``=` `list``(``zip``(``*``test_list))` `# creating list of tuples with sliced summation``sorted_list ``=` `[(col, get_sliced_sum(col, i, j)) ``for` `col ``in` `test_list_transposed]` `# sorting the list of tuples based on sliced summation``sorted_list.sort(key``=``lambda` `x: x[``1``])` `# transposing the sorted list of tuples``sorted_list_transposed ``=` `list``(``zip``(``*``sorted_list))` `# converting list of tuples to list of lists``result ``=` `[``list``(sub) ``for` `sub ``in` `sorted_list_transposed]` `# printing result``print``(``"Sorted List Columnwise : "` `+` `str``(result))`

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, 2, 3, 1), (4, 4, 5, 3), (3, 5, 5, 5), (1, 3, 23, 2)], [5, 9, 10, 26]]```

Time complexity: O(mnlog(n)), where m is the number of rows and n is the number of columns in the original list. This is because the list is transposed and sorted based on the sliced summation of each column using the sorted() function, which has a time complexity of O(n*log(n)).

Auxiliary space: O(m*n), where m is the number of rows and n is the number of columns in the original list. This is because the transposed list and the list of tuples with sliced summation are stored in memory during the execution of the program.

Previous
Next