Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Specific Characters Frequency in String List

Python – Specific Characters Frequency in String List

Given a String list, extract frequency of specific characters in the whole strings list.

Input : test_list = [“neveropen is best for Lazyroar”], chr_list = [‘e’, ‘b’, ‘g’, ‘f’] 
Output : {‘g’: 3, ‘e’: 7, ‘b’: 1, ‘f’ : 2} 
Explanation : Frequency of certain characters extracted. 

Input : test_list = [“neveropen”], chr_list = [‘e’, ‘g’] 
Output : {‘g’: 2, ‘e’: 4} 
Explanation : Frequency of certain characters extracted.

Method #1 : Using join() + Counter()

In this, we concatenate all the strings, and then Counter() performs task of getting all frequencies. Last step is to get only specific characters from List in dictionary using dictionary comprehension.

Python3




# Python3 code to demonstrate working of
# Specific Characters Frequency in String List
# Using join() + Counter()
from collections import Counter
 
# initializing lists
test_list = ["neveropen is best for Lazyroar"]
 
# printing original list
print("The original list : " + str(test_list))
 
# char list
chr_list = ['e', 'b', 'g']
 
# dict comprehension to retrieve on certain Frequencies
res = {key:val for key, val in dict(Counter("".join(test_list))).items() if key in chr_list}
     
# printing result
print("Specific Characters Frequencies : " + str(res))


Output

The original list : ['neveropen is best for Lazyroar']
Specific Characters Frequencies : {'g': 3, 'e': 7, 'b': 1}

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(k), where k is the number of characters in the character list (chr_list). 

Method #2 : Using chain.from_iterable() + Counter() + dictionary comprehension

In this, task of concatenation is done using chain.from_iterable() rather than join(). Rest all tasks are done as above method.

Python3




# Python3 code to demonstrate working of
# Specific Characters Frequency in String List
# Using chain.from_iterable() + Counter() + dictionary comprehension
from collections import Counter
from itertools import chain
 
# initializing lists
test_list = ["neveropen is best for Lazyroar"]
 
# printing original list
print("The original list : " + str(test_list))
 
# char list
chr_list = ['e', 'b', 'g']
 
# dict comprehension to retrieve on certain Frequencies
# from_iterable to flatten / join
res = {key:val for key, val in dict(Counter(chain.from_iterable(test_list))).items() if key in chr_list}
     
# printing result
print("Specific Characters Frequencies : " + str(res))


Output

The original list : ['neveropen is best for Lazyroar']
Specific Characters Frequencies : {'g': 3, 'e': 7, 'b': 1}

Time complexity: O(n), where n is the length of the input string
Auxiliary space: O(k), where k is the length of the character list.

Method #3: Using count() method.count() method returns the number of times a particular element occurs in a sequence.

Python3




# Python3 code to demonstrate working of
# Specific Characters Frequency in String List
 
# initializing lists
test_list = ["neveropen is best for Lazyroar"]
 
# printing original list
print("The original list : " + str(test_list))
 
# char list
chr_list = ['e', 'b', 'g']
 
d=dict()
for i in chr_list:
    d[i]=test_list[0].count(i)
res=d
     
# printing result
print("Specific Characters Frequencies : " + str(res))


Output

The original list : ['neveropen is best for Lazyroar']
Specific Characters Frequencies : {'e': 7, 'b': 1, 'g': 3}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Specific Characters Frequency in String List
import operator as op
# initializing lists
test_list = ["neveropen is best for Lazyroar"]
 
# printing original list
print("The original list : " + str(test_list))
 
# char list
chr_list = ['e', 'b', 'g']
 
d=dict()
for i in chr_list:
    d[i]=op.countOf(test_list[0],i)
res=d
     
# printing result
print("Specific Characters Frequencies : " + str(res))


Output

The original list : ['neveropen is best for Lazyroar']
Specific Characters Frequencies : {'e': 7, 'b': 1, 'g': 3}

Time Complexity: O(N), where n is the length of the given string
Auxiliary Space: O(N)

Method #5: Using loop and conditional statement

  • Initialize the list of strings test_list with a single string.
  • Print the original list test_list.
  • Initialize the list of characters chr_list whose frequency we want to count in the string.
  • Initialize an empty dictionary res to store the result.
  • Join all the strings in test_list into a single string using the join() method and store it in a variable joined_str.
  • Loop through each character in joined_str and count its frequency if it is present in the chr_list.
  • If a character is present in the chr_list and also present in the res dictionary, increment its count by 1. If a character is present in the chr_list but not in the res dictionary, add it to the dictionary with a count of 1.
  • Print the result dictionary res which will contain the frequencies of the specific characters in the original string.

Python3




# initializing lists
test_list = ["neveropen is best for Lazyroar"]
 
# printing original list
print("The original list : " + str(test_list))
 
# char list
chr_list = ['e', 'b', 'g']
 
# initializing dictionary for result
res = {}
 
# loop through each character in the test_list and count their frequency
for char in "".join(test_list):
    if char in chr_list:
        if char in res:
            res[char] += 1
        else:
            res[char] = 1
             
# printing result
print("Specific Characters Frequencies : " + str(res))


Output

The original list : ['neveropen is best for Lazyroar']
Specific Characters Frequencies : {'g': 3, 'e': 7, 'b': 1}

Time complexity: O(n), where n is the length of the string in the test_list. 
Auxiliary space: O(n), since the result dictionary will have at most n/3 key-value pairs if all the characters in the string are from the chr_list.

RELATED ARTICLES

Most Popular

Recent Comments