Often during problem solving we come across to many problems where we need to reverse sort the list. But sometimes we would also want to reverse sort another list so that the elements of are automatically shifted and remain at same index as the first list even after first list get reverse sorted. Let’s discuss certain ways in which this can be done.
Method #1 : Using sorted() + reverse + zip() + itemgetter()
Combining the three functions we can possibly achieve the task. The zip functions binds the two list together, sorted function sorts the list and itemgetter function is used to define the metrics against which we need second list to shift, in this case first list. The reverse sorting is handled by reverse.
Python3
from operator import itemgetter
test_list1 = [ 3 , 4 , 9 , 1 , 6 ]
test_list2 = [ 1 , 5 , 3 , 6 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [ list (x) for x in zip (
* sorted ( zip (test_list1, test_list2), key = itemgetter( 0 ), reverse = True ))]
print ( "The lists after integrity reverse sort : " + str (res))
|
Output : The original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity reverse sort : [[9, 6, 4, 3, 1], [3, 7, 5, 1, 6]]
Method #2: Using sorted() + reverse + zip() + lambda function
This method performs a similar task, each function performing a similar function, the difference is just the instead of itemgetter function, lambda function performs the task of assigning a base to sort the list, i.e the first list in this case. The reverse sorting is handled by reverse.
Python3
from operator import itemgetter
test_list1 = [ 3 , 4 , 9 , 1 , 6 ]
test_list2 = [ 1 , 5 , 3 , 6 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [ list (i) for i in zip ( * sorted ( zip (test_list1, test_list2),
key = lambda dual: dual[ 0 ], reverse = True ))]
print ( "The lists after integrity reverse sort : " + str (res))
|
Output : The original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity reverse sort : [[9, 6, 4, 3, 1], [3, 7, 5, 1, 6]]
Method #3: Using the numpy library to transpose the matrix
Algorithm:
- Create a 2D numpy array using the two lists provided and transpose it to create a row matrix.
- Use numpy.argsort() method to get the indices that would sort the array in ascending order based on the first column.
- Reverse the order of the indices obtained in the previous step to sort the array in descending order.
- Index the original 2D array using the reversed indices obtained in step 3.
- Transpose the sorted 2D array to get the result as two lists.
Python3
import numpy as np
test_list1 = [ 3 , 4 , 9 , 1 , 6 ]
test_list2 = [ 1 , 5 , 3 , 6 , 7 ]
matrix = np.array([test_list1, test_list2]).T
sorted_indices = np.argsort(matrix[:, 0 ])[:: - 1 ]
sorted_matrix = matrix[sorted_indices]
res = sorted_matrix.T.tolist()
print ( "The lists after integrity reverse sort: " + str (res))
|
Output : The original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity reverse sort : [[9, 6, 4, 3, 1], [3, 7, 5, 1, 6]]
Time Complexity: O(nlogn)
This is because we Created a 2D numpy array that takes O(n) time. The argsort() method takes O(nlogn) time complexity, and the reverse() method takes O(n) time complexity. Indexing an array takes O(n) time. Overall, the time complexity is O(nlogn).
Auxiliary Space: O(n)
This is because we are creating a 2D numpy array that requires O(n) space. Sorting the array takes O(n) space. Therefore, the overall space complexity is O(n).
Method #4: Using tuple,sorted,list comprehension
- Initializing two lists test_list1 and test_list2
- Creating a new list called combined that contains tuples with corresponding elements from test_list1 and test_list2 using the zip() function.
- Sorting the combined list in reverse order based on the first element of each tuple using the sorted() function with the key argument as a lambda function that returns the first element of each tuple and reverse argument as True.
- Extracting the sorted lists from the sorted tuples by iterating over the combined_sorted list using a list comprehension and taking the first and second elements of each tuple respectively.
- Printing the result.
Python3
test_list1 = [ 3 , 4 , 9 , 1 , 6 ]
test_list2 = [ 1 , 5 , 3 , 6 , 7 ]
combined = list ( zip (test_list1, test_list2))
combined_sorted = sorted (combined, key = lambda x: x[ 0 ], reverse = True )
test_list1_sorted = [t[ 0 ] for t in combined_sorted]
test_list2_sorted = [t[ 1 ] for t in combined_sorted]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The lists after integrity reverse sort : " + str ([test_list1_sorted, test_list2_sorted]))
|
OutputThe original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity reverse sort : [[9, 6, 4, 3, 1], [3, 7, 5, 1, 6]]
Time complexity: O(nlogn) where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #5: Using a for loop and a dictionary
- Initialize an empty dictionary.
- Use a for loop to iterate over both lists simultaneously using the zip() function.
- In each iteration, add the value from the first list as a key to the dictionary with the value from the second list as its corresponding value.
- Sort the keys of the dictionary in descending order.
- Create two empty lists.
- Use a for loop to iterate over the sorted keys of the dictionary.
- In each iteration, append the key to the first list and its corresponding value to the second list.
- Print the original and sorted lists.
Python3
test_list1 = [ 3 , 4 , 9 , 1 , 6 ]
test_list2 = [ 1 , 5 , 3 , 6 , 7 ]
d = {}
for a, b in zip (test_list1, test_list2):
d[a] = b
sorted_keys = sorted (d.keys(), reverse = True )
test_list1_sorted = []
test_list2_sorted = []
for k in sorted_keys:
test_list1_sorted.append(k)
test_list2_sorted.append(d[k])
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The lists after integrity reverse sort : " + str ([test_list1_sorted, test_list2_sorted]))
|
OutputThe original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity reverse sort : [[9, 6, 4, 3, 1], [3, 7, 5, 1, 6]]
Time complexity: O(n log n) (due to sorting)
Auxiliary space: O(n)