Python – List Words Frequency in String

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Divide String into Equal K chunks
# Using list comprehension
from collections import defaultdict
  
# initializing strings
test_str = 'geeksforgeeks is best for geeks and best for CS'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing count_list 
count_list = ['best', 'geeksforgeeks', '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)) 

chevron_right


Output

The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]

Method #2 : Using Counter() + list comprehension

In this, Counter() is used to perform task of computing frequency, post that, list comprehension is used to asign frequency to list words.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Divide String into Equal K chunks
# Using list comprehension
from collections import Counter
  
# initializing strings
test_str = 'geeksforgeeks is best for geeks and best for CS'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing count_list 
count_list = ['best', 'geeksforgeeks', '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)) 

chevron_right


Output

The original string is : geeksforgeeks is best for geeks and best for CS
The list words frequency : [2, 1, 0, 0, 2, 1]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.