Open In App

# Python | Reverse Sort Row Matrix integration

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

 `# Python3 code to demonstrate``# Reverse Sort Row Matrix integration``# using sorted() + zip() + itemgetter()``from` `operator ``import` `itemgetter` `# initializing lists``test_list1 ``=` `[``3``, ``4``, ``9``, ``1``, ``6``]``test_list2 ``=` `[``1``, ``5``, ``3``, ``6``, ``7``]` `# printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))` `# using sorted() + zip() + itemgetter()``# Reverse Sort Row Matrix integration``res ``=` `[``list``(x) ``for` `x ``in` `zip``(``    ``*``sorted``(``zip``(test_list1, test_list2), key``=``itemgetter(``0``), reverse``=``True``))]` `# printing result``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

 `# Python3 code to demonstrate``# Reverse Sort Row Matrix integration``# using sorted() + zip() + lambda function``from` `operator ``import` `itemgetter` `# initializing lists``test_list1 ``=` `[``3``, ``4``, ``9``, ``1``, ``6``]``test_list2 ``=` `[``1``, ``5``, ``3``, ``6``, ``7``]` `# printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))` `# using sorted() + zip() + lambda function``# Reverse Sort Row Matrix integration``res ``=` `[``list``(i) ``for` `i ``in` `zip``(``*``sorted``(``zip``(test_list1, test_list2),``                                    ``key``=``lambda` `dual: dual[``0``], reverse``=``True``))]` `# printing result``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:

1. Create a 2D numpy array using the two lists provided and transpose it to create a row matrix.
2. Use numpy.argsort() method to get the indices that would sort the array in ascending order based on the first column.
3. Reverse the order of the indices obtained in the previous step to sort the array in descending order.
4. Index the original 2D array using the reversed indices obtained in step 3.
5. Transpose the sorted 2D array to get the result as two lists.

## Python3

 `#Importing the numpy library``import` `numpy as np` `#Initializing the lists``test_list1 ``=` `[``3``, ``4``, ``9``, ``1``, ``6``]``test_list2 ``=` `[``1``, ``5``, ``3``, ``6``, ``7``]` `#Creating a 2D matrix from the two lists``matrix ``=` `np.array([test_list1, test_list2]).T` `#Using numpy argsort function to get the sorted indices``#Reversing the order of the sorted indices to get them in descending order``sorted_indices ``=` `np.argsort(matrix[:, ``0``])[::``-``1``]` `#Getting the sorted matrix using the sorted indices``sorted_matrix ``=` `matrix[sorted_indices]` `#Transposing the matrix to get the two lists back``res ``=` `sorted_matrix.T.tolist()` `#Printing the result``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

 `#Initializing the lists``test_list1 ``=` `[``3``, ``4``, ``9``, ``1``, ``6``]``test_list2 ``=` `[``1``, ``5``, ``3``, ``6``, ``7``]` `# create a list of tuples with corresponding elements from list1 and list2``combined ``=` `list``(``zip``(test_list1, test_list2))` `# sort the list in reverse order based on the first element of each tuple``combined_sorted ``=` `sorted``(combined, key``=``lambda` `x: x[``0``], reverse``=``True``)` `# extract the sorted lists from the sorted tuples``test_list1_sorted ``=` `[t[``0``] ``for` `t ``in` `combined_sorted]``test_list2_sorted ``=` `[t[``1``] ``for` `t ``in` `combined_sorted]` `# print the original and sorted lists``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]))`

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) 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``]` `# create dictionary with corresponding elements from list1 and list2``d ``=` `{}``for` `a, b ``in` `zip``(test_list1, test_list2):``    ``d[a] ``=` `b` `# sort the keys of the dictionary in descending order``sorted_keys ``=` `sorted``(d.keys(), reverse``=``True``)` `# extract the sorted lists from the sorted keys of the dictionary``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 and sorted lists``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]))`

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(n log n) (due to sorting)
Auxiliary space: O(n)