Open In App

Python – Sort rows by Frequency of K

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, the task is to write a Python program to perform sorting on rows depending on the frequency of K.

Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], K = 2 
Output : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]] 
Explanation : 0 < 1 < 2 < 3, count of K in Matrix order.
 

Input : test_list = [[5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], K = 2 
Output : [[5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] 
Explanation : 0 < 1 < 3, count of K in Matrix order. 

Method #1 : Using sort() + count()

In this, we perform the task of in-place sorting using sort(), capturing frequency is done using count()

Python3




# Python3 code to demonstrate working of
# Sort rows by Frequency of K
# Using sort() + count()
 
 
def get_Kfreq(row):
 
    # return Frequency
    return row.count(K)
 
 
# initializing list
test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# performing inplace sort
test_list.sort(key=get_Kfreq)
 
# printing result
print("Sorted List : " + str(test_list))


Output:

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] Sorted List : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]]

Time Complexity: O(m*nlogn)
Auxiliary Space: O(k)

Method #2 : Using sorted() + lambda + count()

In this, we perform the task of sorting using sorted() and lambda, eliminates the external function call and lambda function used for computation.

Python3




# Python3 code to demonstrate working of
# Sort rows by Frequency of K
# Using sorted() + lambda + count()
 
# initializing list
test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# performing inplace sort
res = sorted(test_list, key=lambda row: row.count(K))
 
# printing result
print("Sorted List : " + str(res))


Output:

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] Sorted List : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]]

Time Complexity: O(nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using operator.countOf()

Python3




# Python3 code to demonstrate working of
# Sort rows by Frequency of K
# Using operator.countOf()
import operator as op
 
def get_Kfreq(row):
 
    # return Frequency
    return op.countOf(row,K)
 
 
# initializing list
test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# performing inplace sort
test_list.sort(key=get_Kfreq)
 
# printing result
print("Sorted List : " + str(test_list))


Output

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
Sorted List : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]]

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads