Program to sort matrix by its length of character using Python

by | Apr 14, 2021 | Python Programs

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 to sort matrix by its length of character

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

Program to sort matrix by its length of character 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.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.