Python - Sort by Uppercase Frequency
Last Updated :
08 Mar, 2023
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))
OutputThe original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS']
Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']
Time Complexity: O(n*nlogn), where n is the length of the input list. This is because we’re using the built-in sorted() function which has a time complexity of O(nlogn) in the worst case and isupper has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself.
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))
OutputThe original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS']
Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']
Method #3 : Using Counter
This approach uses the Counter method from the collections module to get the frequency count of uppercase letters in each string and then uses the sorted method to sort the list based on these counts.
Python3
# Method #3 : Using Counter
# Python3 code to demonstrate working of
# Sort by Uppercase Frequency
# Using Counter
from collections import Counter
# initializing list
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
# printing original list
print("The original list is: " + str(test_list))
# Using Counter to get uppercase frequency count for each string
uppercase_counts = [Counter(string)['A']+Counter(string)['B']+Counter(string)['C']+Counter(string)['D']+Counter(string)['E']+Counter(string)['F']+Counter(string)['G']+Counter(string)['H']+Counter(string)['I']+Counter(string)['J']+Counter(string)['K']+Counter(string)['L']+Counter(string)['M']+Counter(string)['N']+Counter(string)['O']+Counter(string)['P']+Counter(string)['Q']+Counter(string)['R']+Counter(string)['S']+Counter(string)['T']+Counter(string)['U']+Counter(string)['V']+Counter(string)['W']+Counter(string)['X']+Counter(string)['Y']+Counter(string)['Z'] for string in test_list]
# Using zip and sorted to sort the list based on uppercase frequency count
res = [x for _, x in sorted(zip(uppercase_counts, test_list))]
# printing result
print("Elements after uppercase sorting: " + str(res))
OutputThe original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS']
Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']
Time Complexity: O(n^2), as for each string in the list, we are checking the frequency of uppercase letters in that string using the Counter method which takes O(n) time, and we are doing it for n strings. So, the total time complexity will be O(n^2).
Auxiliary Space: O(n), as we are using a list of size n to store the uppercase frequency count for each string.
Method#4: using re module
Step-by-step algorithm:
- Define a list of strings to be sorted.
- Define a function uppercase_frequency() that takes a string as input and returns the frequency of uppercase characters in the string. This is done using the findall() function from the re module to find all uppercase letters in the string, and then using the len() function to count the number of matches.
- Use the sorted() function to sort the list of strings based on the result of the uppercase_frequency() function. The key parameter is set to the uppercase_frequency() function to indicate that the sorting should be done based on the result of this function for each string.
- Print the original list and the sorted list.
Python3
import re
# Define a list of strings to be sorted
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
# Define a function to calculate the frequency of uppercase characters in a string
def uppercase_frequency(s):
return len(re.findall(r'[A-Z]', s))
# Use the sorted() function to sort the list of strings by their uppercase frequency
sorted_list = sorted(test_list, key=uppercase_frequency)
# Print the original list and the sorted list
print("The original list is: " + str(test_list))
print("Elements after uppercase sorting: " + str(sorted_list))
OutputThe original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS']
Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']
Time complexity:
The time complexity of the uppercase_frequency() function is O(n), where n is the length of the input string, since it uses the findall() function to search the entire string for uppercase letters. The time complexity of the sorted() function is O(n log n), where n is the length of the list, since it performs a comparison-based sort. Therefore, the overall time complexity of the code is O(n log n).
Auxiliary space:
The auxiliary space complexity of the uppercase_frequency() function is O(n), where n is the length of the input string, since it creates a list of all uppercase letters in the string. The sorted() function uses O(log n) space for the recursive calls in the sorting algorithm, but this is negligible compared to the space used by the input list and the uppercase_frequency() function. Therefore, the overall auxiliary space complexity of the code is O(n).
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read