Python Program to Sort an array of String according frequency

In this post, we will learn how to sort a Python array according to the frequency of each string. For example, let’s consider an array

Input

arr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday',  'Sunday']

Output

[ 'Wednesday', 'Friday', 'Sunday', 'Monday', 'Tuesday', 'Thursday', 'Saturday']

Sorting an array of String according to the frequency

We going to solve this problem in various methods.

Method 1: Using Collections Module

Collections in Python are containers that used to store the collected data in different ways i.e. list, tuple, dict.

1. Import the necessary modules.

2. Counter function in the collections used to count the occurrence of each string in the array.

3. Finally, return the array.

 

Below is our Python code:

from collections import Counter
arr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday','Sunday']

print( "The original array is\n ", str(arr))
result = [key for key , value in Counter(arr).most_common()]

print("The sorted array is \n", str(result))

Output

The original array is
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday', 'Sunday']
The sorted array is 
['Wednesday', 'Friday', 'Sunday', 'Monday', 'Tuesday', 'Thursday', 'Saturday']

Method 2: Using Maps

1. Create a map data structure that stores the string based on their frequency (i.e. frequency, string).

2. Sort these pairs, the higher frequency is sorted before.

arr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday','Sunday']
print( "The original array is\n ", str(arr))
n = len(arr)
m = {arr[i]:0 for i in range(n)}

for i in range(len(arr)):
    m[arr[i]] += 1

vec =[]

for key,value in m.items():
    vec.append([value,key])

sorted_array = sorted(vec, reverse = True)

for i in range(len(sorted_array)):
    print("The sorted array is\n",sorted_array[i][1])

Output

The original array is
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday', 'Sunday']
The sorted array is
Wednesday
Sunday
Friday
Tuesday
Thursday
Saturday
Monday

Method 3: Using count()+set()+sorted()

1. Use function count to calculate the frequency of each string.

2. Use set function to remove the duplicates.

arr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday','Sunday']
print( "The original array is\n ", str(arr))

result = sorted(set(arr),key = lambda ele: arr.count(ele), reverse = True)
print("The sorted array is \n", str(result))

Output

The original array is
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Wednesday', 'Friday', 'Sunday']
The sorted array is 
['Wednesday', 'Friday', 'Sunday', 'Monday', 'Tuesday', 'Thursday', 'Saturday']

Also, read

Leave a Reply

Your email address will not be published.