Skip to content
Related Articles

Related Articles

Python Program to sort rows of a matrix by custom element count

Improve Article
Save Article
Like Article
  • Last Updated : 12 Nov, 2020

Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. 

Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]],  cus_list = [4, 5, 7] 
Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] 
Explanation : 0 < 1 = 1 < 3 is order of custom elements count.
Input : test_list = [[4, 5, 1, 7], [6, 5], [7, 1]],  cus_list = [4, 5, 7] 
Output : [[6, 5], [7, 1], [4, 5, 1, 7]] 
Explanation : 1 = 1 < 3 is order of custom elements count. 
 

Method 1 : Using sort() and len() 

In this, we perform in-place sort using sort(), and check for all elements and increase counter in cases of element presence from custom list, len() returns length to get count.

Python3




# sorting util.
def get_count(sub):
    return len([ele for ele in sub if ele in cus_list])
  
  
# initializing list
test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing custom list
cus_list = [4, 5, 7]
  
# performing inplace sort
test_list.sort(key=get_count)
  
# printing result
print("Sorted Matrix : " + str(test_list))

Output:

The original list is : [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]

Sorted Matrix : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]]

Method 2 : Using sorted(), lambda and len()

Another way of solving this problem is to perform the task of sorting using sorted() and lambda function offers one statement logic without calling external function.

Python3




# initializing list
test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing custom list
cus_list = [4, 5, 7]
  
# performing sort using sorted()
res = sorted(test_list, key=lambda sub: len(
    [ele for ele in sub if ele in cus_list]))
  
# printing result
print("Sorted Matrix : " + str(res))

Output:

The original list is : [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]

Sorted Matrix : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!