How to find the prefix frequency in a list of strings with Python

2 Answers

0 votes
def find_prefix_frequency(lststrings, prefix):
   count = 0
   for string in lststrings:
      if string.startswith(prefix):
         count += 1
         
   return count

lststrings = ['Puzzle', 'Internet', 'Python', 'Games', 'Pyramid', 'Pyjamas']
prefix = 'Py'

print("Frequency of prefix "+ prefix + " is: "+ str(find_prefix_frequency(lststrings, prefix)))


   
'''
run:
 
Frequency of prefix Py is: 3
   
'''

 



answered Feb 10 by avibootz
0 votes
def find_prefix_frequency(lststrings, prefix):
    prefix_strings = [string for string in lststrings if string.startswith(prefix)]

    return len(prefix_strings)

lststrings = ['Puzzle', 'Internet', 'Python', 'Games', 'Pyramid', 'Pyjamas']
prefix = 'Py'

print("Frequency of prefix "+ prefix + " is: "+ str(find_prefix_frequency(lststrings, prefix)))

   
'''
run:
 
Frequency of prefix Py is: 3
   
'''

 



answered Feb 10 by avibootz
...