Given a List of Words, Map frequency of each to occurrence in String.
Input : test_str = 'neveropen is best for Lazyroar and best for CS', count_list = ['best', 'neveropen', 'computer'] Output : [2, 1, 0] Explanation : best has 2 occ., neveropen 1 and computer is not present in string.
Input : test_str = 'neveropen 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 comprehension from collections import defaultdict # Initializing strings test_str = 'neveropen is best for Lazyroar and best for CS' # Printing original string print ( "The original string is : " + str (test_str)) # Initializing count_list count_list = [ 'best' , 'neveropen' , 'computer' , 'better' , 'for' , 'and' ] # Computing frequency res = defaultdict( int ) for sub in test_str.split(): res[sub] + = 1 # Assigning to list words res = [res[sub] for sub in count_list] # Printing result print ( "The list words frequency : " + str (res)) |
The original string is : neveropen 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 comprehension from collections import Counter # initializing strings test_str = 'neveropen is best for Lazyroar and best for CS' # printing original string print ( "The original string is : " + str (test_str)) # initializing count_list count_list = [ 'best' , 'neveropen' , 'computer' , 'better' , 'for' , 'and' ] # computing frequency using Counter() res = Counter(test_str.split()) # assigning to list words res = [res[sub] for sub in count_list] # printing result print ( "The list words frequency : " + str (res)) |
The original string is : neveropen 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 strings test_str = 'neveropen is best for Lazyroar and best for CS' # Printing original string print ( "The original string is : " + str (test_str)) x = test_str.split() # Iitializing count_list count_list = [ 'best' , 'neveropen' , 'computer' , 'better' , 'for' , 'and' ] # Cmputing frequency res = [] for i in count_list: res.append(x.count(i)) # Pinting result print ( "The list words frequency : " + str (res)) |
The original string is : neveropen 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 strings test_str = 'neveropen is best for Lazyroar and best for CS' # printing original string print ( "The original string is : " + str (test_str)) # initializing count_list count_list = [ 'best' , 'neveropen' , 'computer' , 'better' , 'for' , 'and' ] # computing frequency using dictionary comprehension res = {i: test_str.split().count(i) for i in count_list} # printing result print ( "The list words frequency : " + str ([res[i] for i in count_list])) |
The original string is : neveropen 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.