Python - Sort Matrix by Maximum String Length
Last Updated :
10 Apr, 2023
Given a matrix, perform row sort basis on the maximum length of the string in it.
Input : test_list = [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']]
Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']]
Explanation : 3 < 4 < 5 < 13, maximum lengths of strings, sorted increasingly.
Input : test_list = [['gfg', 'best'], ['cs', 'rocks'], ['gfg', 'cs']]
Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks']]
Explanation : 3 < 4 < 5 maximum lengths of strings, sorted increasingly.
Method #1 : Using sort() + len() + max()
In this, in place sorting is performed using sort(), len() and max() to compute the maximum length of the string in each row to perform the sort.
Python3
# Python3 code to demonstrate working of
# Sort Matrix by Maximum String Length
# Using sort() + len() + max()
def max_len(row):
# getting Maximum length of string
return max([len(ele) for ele in row])
# initializing list
test_list = [['gfg', 'best'], ['geeksforgeeks'],
['cs', 'rocks'], ['gfg', 'cs']]
# printing original list
print("The original list is : " + str(test_list))
# performing sort()
test_list.sort(key=max_len)
# printing result
print("Sorted Matrix : " + str(test_list))
Output:
The original list is : [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']] Sorted Matrix : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']]
Time Complexity: O(n)
Auxiliary Space: O(n)
In this, we perform the task of filtering maximum using lambda function rather than the external function. The task of sorting is performed using sorted().
Python3
# Python3 code to demonstrate working of
# Sort Matrix by Maximum String Length
# Using sorted() + lambda + max() + len()
# initializing list
test_list = [['gfg', 'best'], ['geeksforgeeks'],
['cs', 'rocks'], ['gfg', 'cs']]
# printing original list
print("The original list is : " + str(test_list))
# performing logic using lambda fnc.
res = sorted(test_list, key=lambda row: max([len(ele) for ele in row]))
# printing result
print("Sorted Matrix : " + str(res))
Output:
The original list is : [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']] Sorted Matrix : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Using a function with sort():
Approach:
In this approach, we define a function that takes a sublist and returns the maximum length of the strings in that sublist. We use this function as the key parameter in the sort() function to sort the list of sublists by the maximum length of their strings.
Python3
def max_string_length(sublist):
return max(len(s) for s in sublist)
test_list = [['gfg', 'best'], ['cs', 'rocks'], ['gfg', 'cs']]
test_list.sort(key=max_string_length)
print(test_list)
Output[['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks']]
Time Complexity: O(n log n) - where n is the length of the list of sublists
Space Complexity: O(1) - since we modify the original list in place
Similar Reads
Python program to Sort Matrix by Maximum Row element Given a Matrix, sort rows by maximum element. Input : test_list = [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]] Output : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]] Explanation : 18, 10, 8 and 5 are maximum elements in rows, hence sorted. Input : test_list = [[9, 10, 3], [10, 18, 3], [0, 3, 5]
4 min read
Python - Sort Strings by maximum frequency character Given a string, the task is to write a Python program to perform sort by maximum occurring character. Input : test_list = ["geekforgeeks", "bettered", "for", "geeks"] Output : ['for', 'geeks', 'bettered', 'geekforgeeks'] Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurren
3 min read
Python - Sort Matrix by K Sized Subarray Maximum Sum Given Matrix, write a Python program to sort rows by maximum of K sized subarray sum. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]], K = 3 Output : [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [5, 4, 3, 2, 1], [4, 3, 9, 3, 9]] Explanation : 12 = 12 = 12
4 min read
Maximum String Value Length of Key - Python The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
3 min read
Python - Sort Matrix by Palindrome count Given a String Matrix of N characters, sort each row by the count of the palindromic string in it. Input : test_list = [["nitin", "meem", "geeks"], ["peep"], ["gfg", "is", "best"], ["sees", "level", "mom", "noon"]] Output : [['peep'], ['gfg', 'is', 'best'], ['nitin', 'meem', 'geeks'], ['sees', 'leve
8 min read