Python program to find the frequency of the elements which are common in a list of strings
Given list of strings. The task is to find the frequency of the elements which are common in all strings given in the list of strings.
Examples:
Input : test_list = ["gegek", "gfgk", "kingg"] Output : {'g': 2, 'k': 1} Explanation : g occurs twice in all Strings. Input : test_list = ["gefgek", "gfgk", "kinfgg"] Output : {'g': 2, 'k': 1, 'f' : 1} Explanation : f occurs once in all Strings.
Method : Using reduce() + lambda + Counter()
The combination of above functions can be used to solve this problem. In this, we perform the key role of counting using Counter() and lambda coupled with reduce() is used to perform intersection and extension of logic to all the strings respectively.
Python3
# Python3 code to demonstrate working of # Strings list intersection # Using reduce() + lambda + Counter() from functools import reduce from collections import Counter # initializing list test_list = [ "geek" , "gfgk" , "king" ] # printing original list print ( "The original list is : " + str (test_list)) # using & operator to perform intersection res = reduce ( lambda a, b: a & b, (Counter(ele) for ele in test_list[ 1 :]), Counter(test_list[ 0 ])) # printing result print ( "String intersection and frequency : " + str ( dict (res))) |
Output
The original list is : ['geek', 'gfgk', 'king'] String intersection and frequency : {'g': 1, 'k': 1}