Python program to Sort Strings by Punctuation count
Given the Strings list, sort by punctuations count.
Input : test_list = [“gfg@%^”, “is”, “Best!”]
Output : [‘is’, ‘Best!’, ‘gfg@%^’]
Explanation : 0 < 1 < 3, sorted by punctuation count.
Input : test_list = [“gfg@%^”, “Best!”]
Output : [ ‘Best!’, ‘gfg@%^’]
Explanation : 1 < 3, sorted by punctuation count.
Method #1 : Using string.punctuation + sort()
In this, sorting is done using sort() and punctuations are extracted from punctuation pool from string library. Performs inplace sort.
Python3
from string import punctuation
def get_pnc_count(string):
return len ([ele for ele in string if ele in punctuation])
test_list = [ "gfg@%^" , "is" , "Best!" , "fo@#r" , "@#$ge24eks!" ]
print ( "The original list is : " + str (test_list))
test_list.sort(key = get_pnc_count)
print ( "Sorted Strings list : " + str (test_list))
|
Output:
The original list is : ['gfg@%^', 'is', 'Best!', 'fo@#r', '@#$ge24eks!']
Sorted Strings list : ['is', 'Best!', 'fo@#r', 'gfg@%^', '@#$ge24eks!']
Time Complexity: O(n*nlogn)
Auxiliary Space: O(1)
Method #2 : Using sorted() + punctuation + lambda
In this, we perform sort using sorted() using lambda to avoid external function to perform task of filtering punctuations extracted using punctuation.
Python3
from string import punctuation
test_list = [ "gfg@%^" , "is" , "Best!" , "fo@#r" , "@#$ge24eks!" ]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda string: len (
[ele for ele in string if ele in punctuation]))
print ( "Sorted Strings list : " + str (res))
|
Output:
The original list is : [‘gfg@%^’, ‘is’, ‘Best!’, ‘fo@#r’, ‘@#$ge24eks!’]
Sorted Strings list : [‘is’, ‘Best!’, ‘fo@#r’, ‘gfg@%^’, ‘@#$ge24eks!’]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using re
This approach uses a regular expression pattern r'[^\w\s]’ to match any character that is not a word character (letters, digits, and underscores) or whitespace. The re.findall() function returns a list of all non-overlapping matches as strings.
Python3
import re
def get_punctuation_count(string):
return len (re.findall(r '[^\w\s]' , string))
test_list = [ "gfg@%^" , "is" , "Best!" , "fo@#r" , "@#$ge24eks!" ]
print ( "The original list is: " , test_list)
res = sorted (test_list, key = get_punctuation_count)
print ( "Sorted Strings list: " , res)
|
Output
The original list is: ['gfg@%^', 'is', 'Best!', 'fo@#r', '@#$ge24eks!']
Sorted Strings list: ['is', 'Best!', 'fo@#r', 'gfg@%^', '@#$ge24eks!']
Time complexity: O(n)
Auxiliary space: O(n)
Method 4: Use the Counter module from collections.
Step-by-step approach:
- Import the Counter module from collections.
- Define a function that takes a string and returns the count of its punctuation characters using Counter.
- Initialize the list of strings to be sorted.
- Use sorted() function with a lambda function that calls the function defined in step 2 for each string in the list.
- Print the sorted list.
Python3
from string import punctuation
from collections import Counter
def count_punct(string):
return sum (Counter(string) for c in punctuation)
test_list = [ "gfg@%^" , "is" , "Best!" , "fo@#r" , "@#$ge24eks!" ]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda string: count_punct(string))
print ( "Sorted Strings list : " + str (res))
|
Output
The original list is : ['gfg@%^', 'is', 'Best!', 'fo@#r', '@#$ge24eks!']
Sorted Strings list : ['is', 'Best!', 'fo@#r', 'gfg@%^', '@#$ge24eks!']
Time complexity: O(n)
Auxiliary space: O(n)
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...