Python – Sort by Uppercase Frequency
Given a list of strings, perform sorting by frequency of uppercase characters.
Input : test_list = [“Gfg”, “is”, “FoR”, “GEEKS”]
Output : [‘is’, ‘Gfg’, ‘FoR’, ‘GEEKS’]
Explanation : 0, 1, 2, 5 uppercase letters in strings respectively.
Input : test_list = [“is”, “GEEKS”]
Output : [‘is’, ‘GEEKS’]
Explanation : 0, 5 uppercase letters in strings respectively.
Method #1 : Using sort() + isupper()
In this, we perform task of checking for uppercase using isupper(), and sort() to perform task of sorting.
Python3
# Python3 code to demonstrate working of # Sort by Uppercase Frequency # Using isupper() + sort() # helper function def upper_sort(sub): # len() to get total uppercase characters return len ([ele for ele in sub if ele.isupper()]) # initializing list test_list = [ "Gfg" , "is" , "BEST" , "FoR" , "GEEKS" ] # printing original list print ( "The original list is: " + str (test_list)) # using external function to perform sorting test_list.sort(key = upper_sort) # printing result print ( "Elements after uppercase sorting: " + str (test_list)) |
Output:
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']
Method #2 : Using sorted() + lambda function
In this, we perform the task of sorting using sorted(), and lambda function is used rather than external sort() function to perform task of sorting.
Python3
# Python3 code to demonstrate working of # Sort by Uppercase Frequency # Using sorted() + lambda function # initializing list test_list = [ "Gfg" , "is" , "BEST" , "FoR" , "GEEKS" ] # printing original list print ( "The original list is: " + str (test_list)) # sorted() + lambda function used to solve problem res = sorted (test_list, key = lambda sub: len ( [ele for ele in sub if ele.isupper()])) # printing result print ( "Elements after uppercase sorting: " + str (res)) |
Output:
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']