Open In App

Python - List Words Frequency in String

Last Updated : 08 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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
# 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))

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
# 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)) 

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

  1. Split the string test_str which results in a list(x)
  2. Initiate a for loop to traverse the list of strings.
  3. Now append the occurrence of each string in x to the output list.
  4. Display output list.
Python3
# Python3 code to demonstrate working of
# Divide String into Equal K chunks

# Initializing strings
test_str = 'geeksforgeeks is best for geeks 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', 'geeksforgeeks', '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))

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
# Python3 code to demonstrate working of
# Divide String into Equal K chunks


# 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 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]))

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.


Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy