Python – Characters which Occur in More than K Strings

Sometimes, while working with Python, we have a problem in which we compute how many characters are present in string. But sometimes, we can have a problem in which we need to get all characters that occur in aleast K Strings. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using set() + Counter() + items() + loop
The combination of above functions can be used to perform this particular task. In this, we find the count using Counter and set is used to limit the character duplicacy.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Characters which Occur in More than K Strings
# using set() + Counter() + loop + items()
from collections import Counter
from itertools import chain
  
def char_ex(strs, k):
    temp = (set(sub) for sub in strs)
    counts = Counter(chain.from_iterable(temp))
    return {chr for chr, count in counts.items() if count >= k}
  
# Initializing list
test_list = ['Gfg', 'ise', 'for', 'Geeks']
  
# printing original list
print("The original list is : " + str(test_list))
  
# Initializing K
K = 2
  
# Characters which Occur in More than K Strings
# using set() + Counter() + loop + items()
res = char_ex(test_list, K)
      
# printing result 
print ("Filtered Characters are : " + str(res))

chevron_right


Output :

The original list is : ['Gfg', 'ise', 'for', 'Geeks']
Filtered Characters are : {'e', 'G', 's', 'f'}

 

Method #2 : Using dictionary comprehension + set() + Counter()
The combination of these functions perform this task in similar way as above. The difference is just that we use dictionary comprehension to give a compact solution to problem.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Characters which Occur in More than K Strings
# using set() + Counter() + dictionary comprehension
from collections import Counter
  
# Initializing list
test_list = ['Gfg', 'ise', 'for', 'Geeks']
  
# printing original list
print("The original list is : " + str(test_list))
  
# Initializing K
K = 2
  
# Characters which Occur in More than K Strings
# using set() + Counter() + dictionary comprehension
res = {key for key, val in Counter([ele for sub in
          test_list for ele in set(sub)]).items()
          if val >= K}
      
# printing result 
print ("Filtered Characters are : " + str(res))

chevron_right


Output :

The original list is : ['Gfg', 'ise', 'for', 'Geeks']
Filtered Characters are : {'e', 'G', 's', 'f'}



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.