Python | Inverse Sorting String
Last Updated :
05 May, 2023
Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String in increasing order. This is the type of problem which is common in the sorting of pairs. Let's discuss a way in which this can be solved.
Method : Using sorted() + lambda The combination of these functions can be used to perform this task. In these, we pass to lambda function the negative of the values so that the increasing order of number is evaluated as decreasing and hence a successful hack to perform this task.
Python3
# Python3 code to demonstrate working of
# Inverse sorting String, Integer tuple list
# Using sorted() + lambda
# initializing list
test_list = [("Geeks", 5), ("For", 3), ("Geeks", 6), ("Is", 5),
("Best", 7 ), ("For", 5), ("CS", 3)]
# printing original list
print("The original list is : " + str(test_list))
# Inverse sorting String, Integer tuple list
# Using sorted() + lambda
res = sorted(test_list, key = lambda sub: (-sub[1], sub[0]))
# printing result
print("The list after inverse sorted tuple elements : " + str(res))
Output :
The original list is : [('Geeks', 5), ('For', 3), ('Geeks', 6), ('Is', 5), ('Best', 7), ('For', 5), ('CS', 3)] The list after inverse sorted tuple elements : [('Best', 7), ('Geeks', 6), ('For', 5), ('Geeks', 5), ('Is', 5), ('CS', 3), ('For', 3)]
Time Complexity: O(nlogn), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method: Using heapq module
Import the heapq module.
Initialize an empty list result.
Convert the given list into a min heap using the heapify() function of the heapq module.
Extract the elements from the heap one by one using the heappop() function of the heapq module.
Append the extracted element to the result list.
Return the result list.
Python3
import heapq
# initializing list
test_list = [("Geeks", 5), ("For", 3), ("Geeks", 6), ("Is", 5),
("Best", 7 ), ("For", 5), ("CS", 3)]
# printing original list
print("The original list is : " + str(test_list))
# Inverse sorting String, Integer tuple list
# Using heapq module
result = []
heapq.heapify(test_list)
while test_list:
result.append(heapq.heappop(test_list))
# printing result
print("The list after inverse sorted tuple elements : " + str(result))
OutputThe original list is : [('Geeks', 5), ('For', 3), ('Geeks', 6), ('Is', 5), ('Best', 7), ('For', 5), ('CS', 3)]
The list after inverse sorted tuple elements : [('Best', 7), ('CS', 3), ('For', 3), ('For', 5), ('Geeks', 5), ('Geeks', 6), ('Is', 5)]
The time complexity of this approach is O(n log n) due to the heapification step.
The auxiliary space used is O(n) to store the result list.
Similar Reads
sort() in Python sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. Example:Pythona = [5, 3, 8, 1, 2] a.sort() print(a) a.sort(reverse=True) print(a)Output[1
4 min read
sort() in Python sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. Example:Pythona = [5, 3, 8, 1, 2] a.sort() print(a) a.sort(reverse=True) print(a)Output[1
4 min read
Python | Sorting string using order defined by another string Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat =
2 min read
Python | Pandas Index.sort_values() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.sort_values() function is used to sort the index values. The function ret
2 min read
Python | Pandas Index.sort_values() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.sort_values() function is used to sort the index values. The function ret
2 min read
Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat
2 min read