Python – Reverse Row sort in Lists of List
Last Updated :
03 May, 2023
Sometimes, while working with data, we can have a problem in which we need to perform the sorting of rows of the matrix in descending order. This kind of problem has its application in the web development and Data Science domain. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop + sort() + reverse This problem can be solved using a loop to loop over each row. The sort and reverse can be used to perform the reverse sort of rows.
Python3
test_list = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print ( "The original list is : " + str (test_list))
for ele in test_list:
ele.sort(reverse = True )
print ( "The reverse sorted Matrix is : " + str (test_list))
|
Output
The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Method #2: Using list comprehension + sorted() This is yet another way in which this task can be performed. In this, we perform in a similar way, just pack the logic in one line using list comprehension to provide a compact alternative.
Python3
test_list = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print ( "The original list is : " + str (test_list))
res = [ sorted (sub, reverse = True ) for sub in test_list]
print ( "The reverse sorted Matrix is : " + str (res))
|
Output
The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method#3: Using map() + sorted This is one way to solve this problem. In this, we update the existing list with the help of map function which sorts the internal list in reverse order using the sorted function.
Python3
test_list = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print ( "The original list is : " + str (test_list))
res = list ( map ( lambda x: sorted (x, reverse = True ), test_list))
print ( "The reverse sorted Matrix is : " + str (res))
|
Output
The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method #4 : Using sort()+slicing
Python3
test_list = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
ele.sort()
res.append(ele[:: - 1 ])
print ( "The reverse sorted Matrix is : " + str (res))
|
Output
The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Approach #5: Using map() function and lambda expression
The approach applies the sorted function with the reverse=True parameter to each row of the input matrix using map function and returns a list of lists as a result.
Step-by-step approach:
1. Apply the map() function to the input list of lists.
2. Apply a lambda expression to each row of the matrix to sort the row in descending order.
3. Convert the map object into a list and return it.
Python3
def reverse_sort_matrix(matrix):
return list ( map ( lambda row: sorted (row, reverse = True ), matrix))
matrix = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print (reverse_sort_matrix(matrix))
|
Output
[[6, 4, 1], [8, 7], [10, 8, 4]]
Time complexity: O(n^2 log n), where n is the number of elements in the matrix. The sorted() function has a time complexity of O(n log n), and it is being applied n times in this algorithm.
Auxiliary Space: O(n), where n is the number of elements in the matrix. We are creating a new matrix of the same size as the input matrix.
Method #6: Using the heapq module
Step-by-step approach:
- Import the heapq module using import heapq.
- Initialize the input list of lists test_list with some values.
- Print the original list test_list.
- Iterate over each sublist in the input list using a for loop.
- Apply the heapify() function of the heapq module on each sublist to convert it into a min-heap.
- Use a list comprehension to extract the minimum element from the heap using heappop() function in reverse order.
- Append the extracted elements to the result list res.
- Print the result list.
Python3
import heapq
test_list = [[ 4 , 1 , 6 ], [ 7 , 8 ], [ 4 , 10 , 8 ]]
print ( "The original list is : " + str (test_list))
res = []
for sublist in test_list:
heapq.heapify(sublist)
res.append([heapq.heappop(sublist) for i in range ( len (sublist))][:: - 1 ])
print ( "The reverse sorted Matrix is : " + str (res))
|
Output
The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]
Time complexity: O(N * M * logM) where N is the number of sublists and M is the maximum length of the sublists.
Auxiliary space: O(M) where M is the maximum length of the sublists.
Please Login to comment...