Python – Sort String list by K character frequency
Last Updated :
01 May, 2023
Given String list, perform sort operation on basis of frequency of particular character.
Input : test_list = [“geekforgeekss”, “is”, “bessst”, “for”, “geeks”], K = ‘s’
Output : [‘bessst’, ‘geekforgeekss’, ‘geeks’, ‘is’, ‘for’]
Explanation : bessst has 3 occurrence, geeksforgeekss has 3, and so on.
Input : test_list = [“geekforgeekss”, “is”, “bessst”], K = ‘e’
Output : [“geekforgeekss”, “bessst”, “is”]
Explanation : Ordered decreasing order of ‘e’ count.
Method #1 : Using sorted() + count() + lambda
In this, sorted() is used to perform task of sort, count() is as function upon which sorting is to be performed. using additional key param, and function encapsulation used is lambda.
Python3
test_list = [ "geekforgeeks" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
res = sorted (test_list, key = lambda ele: - ele.count(K))
print ( "Sorted String : " + str (res))
|
Output
The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']
Time Complexity: O(nlogn), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using sort() + count() + lambda
In this, we perform task of sort using sort(), this is similar to above, only difference being that sorting is done inplace.
Python3
test_list = [ "geekforgeeks" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
test_list.sort(key = lambda ele: - ele.count(K))
print ( "Sorted String : " + str (test_list))
|
Output
The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using operator.countOf() method
Python3
import operator as op
test_list = [ "geekforgeeks" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
res = sorted (test_list, key = lambda ele: - op.countOf(ele,K))
print ( "Sorted String : " + str (res))
|
Output
The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']
Time Complexity: O(NLogN)
Auxiliary Space: O(N)
Method 4: Using heapq.nlargest() and count()
Step-by-step approach:
- Use the nlargest() function from the heapq module to return the n largest elements from a list of strings, sorted in descending order by the count of the target character K in each string. Set n to the length of the test_list to return all elements.
- Define a lambda function that takes a string as input and returns the count of the target character K in that string.
- Use the count() method to count the number of occurrences of K in each string in the test_list.
- Use the sorted() function to sort the test_list based on the count of K in each string. The key parameter of sorted() should be the lambda function defined in step 2.
- Return the sorted list of strings.
Python3
import heapq
test_list = [ "geekforgeeks" , "is" , "best" , "for" , "geeks" ]
K = 'e'
count_K = lambda s: s.count(K)
n = len (test_list)
sorted_list = heapq.nlargest(n, test_list, key = count_K)
sorted_list = sorted (test_list, key = count_K, reverse = True )
print ( "Sorted String: " , sorted_list)
|
Output
Sorted String: ['geekforgeeks', 'geeks', 'best', 'is', 'for']
Time complexity: O(n*log(n)) for sorting the list of strings.
Auxiliary space: O(n) for storing the list of strings.
Please Login to comment...