Table of Contents
Introduction
The task is to sort the given matrix by the total numbers of the characters in it i.e. length of the row.
Program
input_str = [["It", "is", "easy"], ["Thank", "You", "GoCoding"], ["Python"]] print("The original list is : "+str(input_str)) # USing sorted() and lambda function output = sorted(input_str, key = lambda row : sum([len(i) for i in row])) print("Sorted matrix : "+str(output))
Output
Explanation
In the above python code. We have used sorted and lambda function. As we know the sorted () function is used to sort the given input. Hence we used it to sort our input. And lambda function gives the length of the rows.
0 Comments