Python – Specific Characters Frequency in String List
Given String list, extract frequency of specific characters in whole strings list.
Input : test_list = [“geeksforgeeks is best for geeks”], chr_list = [‘e’, ‘b’, ‘g’, ‘f’]
Output : {‘g’: 3, ‘e’: 7, ‘b’: 1, ‘f’ : 1}
Explanation : Frequency of certain characters extracted.Input : test_list = [“geeksforgeeks], 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 liststest_list = ["geeksforgeeks is best for geeks"] # printing original listprint("The original list : " + str(test_list)) # char list chr_list = ['e', 'b', 'g'] # dict comprehension to retrieve on certain Frequenciesres = {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)) |
The original list : ['geeksforgeeks is best for geeks']
Specific Characters Frequencies : {'g': 3, 'e': 7, 'b': 1}
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 comprehensionfrom collections import Counterfrom itertools import chain # initializing liststest_list = ["geeksforgeeks is best for geeks"] # printing original listprint("The original list : " + str(test_list)) # char list chr_list = ['e', 'b', 'g'] # dict comprehension to retrieve on certain Frequencies# from_iterable to flatten / joinres = {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)) |
The original list : ['geeksforgeeks is best for geeks']
Specific Characters Frequencies : {'g': 3, 'e': 7, 'b': 1}


.png)
