Python – List Words Frequency in String

Given a List of Words, Map frequency of each to occurrence in String.
Input : test_str = 'zambiatek is best for Lazyroar and best for CS', count_list = ['best', 'zambiatek', 'computer'] Output : [2, 1, 0] Explanation : best has 2 occ., zambiatek 1 and computer is not present in string.
Input : test_str = 'zambiatek is best for Lazyroar 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
# Python3 code to demonstrate working of# Divide String into Equal K chunks# Using list comprehensionfrom collections import defaultdict# Initializing stringstest_str = 'zambiatek is best for Lazyroar and best for CS'# Printing original stringprint("The original string is : " + str(test_str))# Initializing count_listcount_list = ['best', 'zambiatek', 'computer', 'better', 'for', 'and']# Computing frequencyres = defaultdict(int)for sub in test_str.split():    res[sub] += 1# Assigning to list wordsres = [res[sub] for sub in count_list]# Printing resultprint("The list words frequency : " + str(res)) | 
The original string is : zambiatek is best for Lazyroar 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
# Python3 code to demonstrate working of # Divide String into Equal K chunks# Using list comprehensionfrom collections import Counter# initializing stringstest_str = 'zambiatek is best for Lazyroar and best for CS'# printing original stringprint("The original string is : " + str(test_str))# initializing count_list count_list = ['best', 'zambiatek', 'computer', 'better', 'for', 'and']# computing frequency using Counter()res = Counter(test_str.split())     # assigning to list wordsres = [res[sub] for sub in count_list]# printing result print("The list words frequency : " + str(res))  | 
The original string is : zambiatek is best for Lazyroar 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
# Python3 code to demonstrate working of# Divide String into Equal K chunks# Initializing stringstest_str = 'zambiatek is best for Lazyroar and best for CS'# Printing original stringprint("The original string is : " + str(test_str))x=test_str.split()# Iitializing count_listcount_list = ['best', 'zambiatek', 'computer', 'better', 'for', 'and']# Cmputing frequencyres=[]for i in count_list:    res.append(x.count(i))# Pinting resultprint("The list words frequency : " + str(res)) | 
The original string is : zambiatek is best for Lazyroar 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
# Python3 code to demonstrate working of# Divide String into Equal K chunks# initializing stringstest_str = 'zambiatek is best for Lazyroar and best for CS'# printing original stringprint("The original string is : " + str(test_str))# initializing count_listcount_list = ['best', 'zambiatek', 'computer', 'better', 'for', 'and']# computing frequency using dictionary comprehensionres = {i: test_str.split().count(i) for i in count_list}# printing resultprint("The list words frequency : " + str([res[i] for i in count_list])) | 
The original string is : zambiatek is best for Lazyroar 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.
				
					


