Python program to Sort a List of Strings by the Number of Unique Characters
Last Updated :
22 Apr, 2023
Given a list of strings. The task is to sort the list of strings by the number of unique characters.
Examples:
Input : test_list = ['gfg', 'best', 'for', 'geeks'],
Output : ['gfg', 'for', 'best', 'geeks']
Explanation : 2, 3, 4, 4 are unique elements in lists.
Input : test_list = ['gfg', 'for', 'geeks'],
Output : ['gfg', 'for', 'geeks']
Explanation : 2, 3, 4 are unique elements in lists.
Method #1 : Using sort() + len() + set()
In this, we perform task of sorting using sort(), and len and sort functions are used to get length of unique characters in string.
Python3
# Python3 code to demonstrate working of
# Sort Strings by Unique characters
# Using sort() + len() + set()
# helper function
def hlper_fnc(ele):
# getting Unique elements count
return len(list(set(ele)))
# initializing list
test_list = ['gfg', 'best', 'for', 'geeks']
# printing original list
print("The original list is : " + str(test_list))
# perform sort
test_list.sort(key = hlper_fnc)
# printing result
print("Sorted List : " + str(test_list))
OutputThe original list is : ['gfg', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'best', 'geeks']
Time Complexity: O(nlogn)
Space Complexity: O(n)
Method #2 : Using sorted() + len() + set() + lambda
Similar to above method, difference being not inplace sort, and also uses lambda function for performing task.
Python3
# Python3 code to demonstrate working of
# Sort Strings by Unique characters
# Using sorted() + len() + set() + lambda
# initializing list
test_list = ['gfg', 'best', 'for', 'geeks']
# printing original list
print("The original list is : " + str(test_list))
# perform sort
res = sorted(test_list, key = lambda sub : len(list(set(sub))))
# printing result
print("Sorted List : " + str(res))
OutputThe original list is : ['gfg', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'best', 'geeks']
Time Complexity: O(nlogn)
Space Complexity: O(n)
Python3
# Python3 code to demonstrate working of
# Sort Strings by Unique characters
# Using sort() + len() + set()
# helper function
def hlper_fnc(ele):
# getting Unique elements count
return len(list(set(ele)))
# initializing list
test_list = ['gfg', 'best', 'for', 'geeks']
# printing original list
print("The original list is : " + str(test_list))
# perform sort
test_list = sorted(test_list, key=hlper_fnc)
# printing result
print("Sorted List : " + str(test_list))
OutputThe original list is : ['gfg', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'best', 'geeks']
Method 4:Using Counter() function
Python3
# Python3 code to demonstrate working of
# Sort Strings by Unique characters
from collections import Counter
# helper function
def hlper_fnc(ele):
# getting Unique elements
freq = Counter(ele)
# getting Unique elements count
return len(freq)
# initializing list
test_list = ['gfg', 'best', 'for', 'geeks']
# printing original list
print("The original list is : " + str(test_list))
# perform sort
test_list.sort(key=hlper_fnc)
# printing result
print("Sorted List : " + str(test_list))
OutputThe original list is : ['gfg', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'best', 'geeks']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method 5: Using NumPy
Here's an approach using NumPy, which creates a NumPy array from the input list and then sorts the array based on the number of unique characters in each string.
Python3
import numpy as np
def sort_list_unique_chars(lst):
# Create a NumPy array from the input list
arr = np.array(lst)
# Get the length of unique characters in each string
unique_char_count = np.array([len(np.unique(i)) for i in arr])
# Sort the NumPy array based on the unique character count
sorted_indices = np.argsort(unique_char_count)
sorted_arr = arr[sorted_indices]
# Convert the sorted NumPy array back to a list
sorted_list = sorted_arr.tolist()
return sorted_list
# Example usage
test_list = ['gfg', 'best', 'for', 'geeks']
print(sort_list_unique_chars(test_list))
Output:
['gfg', 'for', 'best', 'geeks']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method 6: Using QuickSort and count of unique characters
- Define a helper function called count_unique_chars that takes a string and returns the count of unique characters in the string using the set and len functions.
- Define a partition function called partition that takes a list, a low index, and a high index, and returns the index of the pivot element after partitioning the list around the pivot element. The pivot element is the first element in the list (arr[low]). We set two pointers i and j initially pointing to the low and high indices, respectively. We then iterate through the list using the two pointers, swapping elements if necessary, until the pointers cross each other. Finally, we swap the pivot element with the element at index j and return j.
- Define a quicksort function called quicksort that takes a list, a low index, and a high index. If the low index is less than the high index, we partition the list around a pivot index using the partition function and recursively call quicksort on the two resulting sublists to sort them.
- Initialize a test list of strings called test_list.
- Call quicksort on test_list, passing in 0 as the low index and len(test_list)-1 as the high index, to sort the list based on count of unique characters.
- Print the sorted list using the print function.
Python3
# Python3 code to demonstrate working of
# Sort Strings by Unique characters
# Using QuickSort and count of unique characters
# helper function to return count of unique characters in a string
def count_unique_chars(s):
return len(set(s))
# partition function for QuickSort
def partition(arr, low, high):
pivot = arr[low]
i = low + 1
j = high
while True:
while i <= j and count_unique_chars(arr[i]) <= count_unique_chars(pivot):
i += 1
while i <= j and count_unique_chars(arr[j]) >= count_unique_chars(pivot):
j -= 1
if i <= j:
arr[i], arr[j] = arr[j], arr[i]
else:
break
arr[low], arr[j] = arr[j], arr[low]
return j
# QuickSort algorithm to sort list based on count of unique characters
def quicksort(arr, low, high):
if low < high:
pivot_index = partition(arr, low, high)
quicksort(arr, low, pivot_index - 1)
quicksort(arr, pivot_index + 1, high)
# initializing list
test_list = ['gfg', 'best', 'for', 'geeks']
# use QuickSort algorithm to sort list based on count of unique characters
quicksort(test_list, 0, len(test_list) - 1)
# print sorted list
print(test_list)
Output['gfg', 'for', 'geeks', 'best']
The time complexity of the partition function is O(n), and the quicksort function calls the partition function recursively on each sub-list, so the time complexity of quicksort is O(n log n) in the average case and O(n^2) in the worst case.
Auxiliary Space:
The partition function uses a constant amount of auxiliary space, and quicksort uses O(log n) auxiliary space in the average case and O(n) in the worst case due to the recursion depth. Therefore, the overall space complexity of this method is O(log n) in the average case and O(n) in the worst case.
Similar Reads
Python program to list Sort by Number value in String
Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4
6 min read
Sort given list of strings by part the numeric part of string - Python
We are given a list of strings containing both letters and numbers, and the goal is to sort them based on the numeric part within each string. To do this, we extract the digits, convert them to integers and use them as sorting keys. For example, in ["Gfg34", "is67", "be3st"], the numbers 34, 67, and
3 min read
Python Program to Extract Strings with at least given number of characters from other list
Given a list containing only string elements, the task is to write a Python program to extract all the strings which have characters from another list given a number of times. Examples: Input : test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"], char_list = ['e', 't', 's', 'm', 'n'], K = 2
7 min read
Python program to calculate the number of digits and letters in a string
In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Python Program to Sort A List Of Names By Last Name
Given a list of names, the task is to write a Python program to sort the list of names by their last name. Examples: Input: ['John Wick', 'Jason Voorhees'] Output: ['Jason Voorhees', 'John Wick'] Explanation: V in Voorhees of Jason Voorhees is less than W in Wick of John Wick. Input: ['Freddy Kruege
3 min read
Count the number of characters in a String - Python
The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is
2 min read
Python Program To Find Length Of The Longest Substring Without Repeating Characters
Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below
6 min read
Python program to find the character position of Kth word from a list of strings
Given a list of strings. The task is to find the index of the character position for the word, which lies at the Kth index in the list of strings. Examples: Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 21 Output : 0Explanation : 21st index occurs in "geeks" and point to "g
3 min read
Python - Sort list of numbers by sum of their digits
Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w
2 min read
Python Program to Find the Number of Unique Words in Text File
Given a text file, write a python program to find the number of unique words in the given text file in Python.Examples:Input: gfg.txtOutput: 18Contents of gfg.txt: GeeksforGeeks was created with a goal in mind to provide well written well thought and wellexplained solutions for selected questionsExp
2 min read