Python – Words Frequency in String Shorthands
Last Updated :
10 May, 2023
Sometimes while working with Python strings, we can have a problem in which we need to extract the frequency of all the words in a string. This problem has been solved earlier. This discusses the shorthands to solve this problem as this has applications in many domains ranging from web development and competitive programming. Let’s discuss certain ways in which this problem can be solved.
Input : test_str = 'Gfg is best'
Output : {'Gfg': 1, 'is': 1, 'best': 1}
Input : test_str = 'Gfg Gfg Gfg'
Output : {'Gfg': 3}
Method #1: Using dictionary comprehension + count() + split()
The combination of the above functions can be used to solve this problem. In this, we first split all the words and then perform a count of them using count() method.
Python3
test_str = 'Gfg is best . Geeks are good and Geeks like Gfg'
print ( "The original string is : " + str (test_str))
res = {key: test_str.count(key) for key in test_str.split()}
print ( "The words frequency : " + str (res))
|
Output :
The original string is : Gfg is best . Geeks are good and Geeks like Gfg The words frequency : {‘Gfg’: 2, ‘is’: 1, ‘best’: 1, ‘.’: 1, ‘Geeks’: 2, ‘are’: 1, ‘good’: 1, ‘and’: 1, ‘like’: 1}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using Counter() + split()
The combination of the above methods can also be used to solve this problem. In this, we perform the task of counting using Counter() and separation of words using split().
Python3
from collections import Counter
test_str = 'Gfg is best . Geeks are good and Geeks like Gfg'
print ( "The original string is : " + str (test_str))
res = Counter(test_str.split())
print ( "The words frequency : " + str ( dict (res)))
|
Output :
The original string is : Gfg is best . Geeks are good and Geeks like Gfg The words frequency : {‘Gfg’: 2, ‘is’: 1, ‘best’: 1, ‘.’: 1, ‘Geeks’: 2, ‘are’: 1, ‘good’: 1, ‘and’: 1, ‘like’: 1}
Method #3 : Using dictionary comprehension + operator.countOf() + split() :
The combination of the above functions can be used to solve this problem. In this, we first split all the words and then perform count of them using operator.countOf() method.
Python3
import operator as op
test_str = 'Gfg is best . Geeks are good and Geeks like Gfg'
print ( "The original string is : " + str (test_str))
listString = test_str.split()
res = {key: op.countOf(listString, key) for key in listString}
print ( "The words frequency : " + str (res))
|
Output
The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'Gfg': 2, 'is': 1, 'best': 1, '.': 1, 'Geeks': 2, 'are': 1, 'good': 1, 'and': 1, 'like': 1}
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #4: Using defaultdict
Python3
from collections import defaultdict
test_str = 'Gfg is best . Geeks are good and Geeks like Gfg'
print ( "The original string is : " + str (test_str))
listString = test_str.split()
freq = defaultdict( int )
for word in listString:
freq[word] + = 1
res = dict (freq)
print ( "The words frequency : " + str (res))
|
Output
The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'Gfg': 2, 'is': 1, 'best': 1, '.': 1, 'Geeks': 2, 'are': 1, 'good': 1, 'and': 1, 'like': 1}
Time Complexity: O(n), where n is the number of words in the string.
Auxiliary Space: O(n), where n is the number of unique words in the string.
Method #5: Using set() and list comprehension
Step-by-step approach:
- Split the string into a list of words.
- Use set() to get a unique set of words.
- Use a list comprehension to count the frequency of each word in the original list.
- Store the results in a dictionary using dictionary comprehension.
- Print the final result.
Python3
test_str = 'Gfg is best . Geeks are good and Geeks like Gfg'
print ( "The original string is : " + str (test_str))
listString = test_str.split()
freq = {word: listString.count(word) for word in set (listString)}
print ( "The words frequency : " + str (freq))
|
Output
The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'are': 1, 'good': 1, 'and': 1, 'like': 1, 'best': 1, 'Gfg': 2, 'is': 1, 'Geeks': 2, '.': 1}
Time complexity: O(n^2) where n is the length of the list of words.
Auxiliary space: O(n) where n is the length of the list of words.
Please Login to comment...