Python – List Words Frequency in String
Last Updated :
08 May, 2023
Given a List of Words, Map frequency of each to occurrence in String.
Input : test_str = 'geeksforgeeks is best for geeks and best for CS', count_list = ['best', 'geeksforgeeks', 'computer']
Output : [2, 1, 0]
Explanation : best has 2 occ., geeksforgeeks 1 and computer is not present in string.
Input : test_str = 'geeksforgeeks is best for geeks and best for CS', count_list = ['better', 'gfg', 'computer']
Output : [0, 0, 0]
Explanation : No word from list present in string.
Method #1 : Using defaultdict() + loop + list comprehension
In this, we compute words frequency using loop + defaultdict() and then use list comprehension to get all the counts corresponding to list of words.
Python3
from collections import defaultdict
test_str = 'geeksforgeeks is best for geeks and best for CS'
print ( "The original string is : " + str (test_str))
count_list = [ 'best' , 'geeksforgeeks' , 'computer' , 'better' , 'for' , 'and' ]
res = defaultdict( int )
for sub in test_str.split():
res[sub] + = 1
res = [res[sub] for sub in count_list]
print ( "The list words frequency : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the list.
Method #2 : Using Counter() + list comprehension
In this, Counter() is used to perform the task of computing frequency, post that, list comprehension is used to assign a frequency to list words.
Python3
from collections import Counter
test_str = 'geeksforgeeks is best for geeks and best for CS'
print ( "The original string is : " + str (test_str))
count_list = [ 'best' , 'geeksforgeeks' , 'computer' , 'better' , 'for' , 'and' ]
res = Counter(test_str.split())
res = [res[sub] for sub in count_list]
print ( "The list words frequency : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]
Time complexity: O(N) since using a loop
Auxiliary Space: O(1)
Method #3 : Using count() method
Approach
- Split the string test_str which results in a list(x)
- Initiate a for loop to traverse the list of strings.
- Now append the occurrence of each string in x to the output list.
- Display output list.
Python3
test_str = 'geeksforgeeks is best for geeks and best for CS'
print ( "The original string is : " + str (test_str))
x = test_str.split()
count_list = [ 'best' , 'geeksforgeeks' , 'computer' , 'better' , 'for' , 'and' ]
res = []
for i in count_list:
res.append(x.count(i))
print ( "The list words frequency : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]
Time Complexity : O(M*N) M – length of x N – length of count_list
Auxiliary Space : O(N) N – length of output list
Method #4: Using dictionary comprehension
In this method, we can use a dictionary comprehension to count the frequency of each word in the given string. The keys of the dictionary will be the words from the count_list, and the values will be the frequency of each word in the given string.
Python3
test_str = 'geeksforgeeks is best for geeks and best for CS'
print ( "The original string is : " + str (test_str))
count_list = [ 'best' , 'geeksforgeeks' , 'computer' , 'better' , 'for' , 'and' ]
res = {i: test_str.split().count(i) for i in count_list}
print ( "The list words frequency : " + str ([res[i] for i in count_list]))
|
Output
The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]
Time complexity: O(N), where n is the length of the given string.
Auxiliary space: O(K), where k is the number of words in the count_list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...