Skip to content
Related Articles

Related Articles

Python – Sort String list by K character frequency

Improve Article
Save Article
  • Last Updated : 24 Aug, 2022
Improve Article
Save Article

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




# Python3 code to demonstrate working of
# Sort String list by K character frequency
# Using sorted() + count() + lambda
 
# initializing list
test_list = ["geekforgeeks", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'e'
 
# "-" sign used to reverse sort
res = sorted(test_list, key = lambda ele: -ele.count(K))
 
# printing results
print("Sorted String : " + str(res))

Output

The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']

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




# Python3 code to demonstrate working of
# Sort String list by K character frequency
# Using sort() + count() + lambda
 
# initializing list
test_list = ["geekforgeeks", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'e'
 
# "-" sign used to reverse sort
# inplace sort
test_list.sort(key = lambda ele: -ele.count(K))
 
# printing results
print("Sorted String : " + str(test_list))

Output

The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!