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

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.defget_count(sub):    returnlen([ele forele insub ifele incus_list])# initializing listtest_list =[[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]# printing original listprint("The original list is : "+str(test_list))# initializing custom listcus_list =[4, 5, 7]# performing inplace sorttest_list.sort(key=get_count)# printing resultprint("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]]
Time Complexity: O(nlogn+mlogm)
Auxiliary Space: O(k)
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 listtest_list =[[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]# printing original listprint("The original list is : "+str(test_list))# initializing custom listcus_list =[4, 5, 7]# performing sort using sorted()res =sorted(test_list, key=lambdasub: len(    [ele forele insub ifele incus_list]))# printing resultprint("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]]
Time Complexity: O(nlogn+mlogm) where n and m is the number of rows and columns in the list 
Auxiliary Space: O(k) additional space of size k is created where n is the number of elements in the list 
Method 3 : use a custom sorting algorithm.
steps for the approach:
Initialize an empty list to store the sorted sublists.
Loop through each sublist in the original list.
Count the number of elements in the sublist that are also in the custom list.
Create a tuple with the count and the sublist and append it to the empty list.
Sort the list of tuples by the count in ascending order.
Extract the sorted sublists from the sorted tuples and return the final result.
Python3
| # initializing listtest_list =[[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]# initializing custom listcus_list =[4, 5, 7]# custom sorting algorithmdefcustom_sort(sublist):    count =sum(1forele insublist ifele incus_list)    returncount# creating a list of tuples with count and sublisttuples_list =[(custom_sort(sublist), sublist) forsublist intest_list]# sorting the list of tuples by count in ascending ordertuples_list.sort()# extracting the sorted sublists from the tuplessorted_list =[sublist for(count, sublist) intuples_list]# printing resultprint("Sorted Matrix : "+str(sorted_list)) | 
Sorted Matrix : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]]
The time complexity of this approach is O(n * m * log(n)), where n is the number of sublists and m is the maximum length of a sublist.
 The auxiliary space used by this approach is O(n * m), where n is the number of sublists and m is the maximum length of a sublist.
Method 4: Using list comprehension and key parameter
Initialize a list comprehension that iterates over each sublist in the test_list and filters out any elements that are not in the cus_list. Then, use len() to count the number of remaining elements in the sublist.
Use the sorted() function to sort the sublists based on the count of remaining elements, using the key parameter to pass in the list comprehension as the sorting criterion.
Assign the sorted sublists to the variable sorted_list.
Print the sorted sublists using the print() function.
Python3
| test_list =[[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]]cus_list =[4, 5, 7]sorted_list =sorted(test_list, key=lambdax: len([i fori inx ifi incus_list]))print("Sorted Matrix : "+str(sorted_list)) | 
Sorted Matrix : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]]
Time complexity: O(n^2), where n is the length of test_list since we are using list comprehension to iterate over each sublist and filter out elements.
Auxiliary space: O(n), where n is the length of test_list since we are storing the sorted sublists in a new list.
 
				 
					


