Word Frequency in a Python String | Multiple Techniqes – My programming school

[ad_1]

Word Frequency in a Python String | Multiple Techniqes – My programming school. This tutorial supplies a number of strategies to depend the frequency of every phrase in a Python string, adopted by easy examples.

Here, now we have to write down a Python program that may settle for a string as enter and calculate the prevalence of every phrase in it. We can tackle this downside with many programming logic. Let’s discover out every resolution one after the other.

Python Program – Compute Word Frequency in a String

It is at all times thrilling to unravel a downside by taking numerous approaches. An actual programmer retains attempting and considers doing issues in a higher means.

Using List to depend the phrase frequency in a string

Let’s see how can we use a listing to depend the occurrences of every phrase in a string. Following is the step-by-step element:

  • The very first thing, we’ll do it is to transform the string to a listing. Python string has a cut up() technique. It takes a string and some separator (really a area in our case) to return a listing.
  • Next, we’ll want to make use of one other listing that will likely be empty initially.
  • After that, we’ll retailer distinctive values of the primary listing into the second.
  • Finally, we’ll use the Python range to iterate string listing having distinctive values that imply inside a loop.
  • In the loop, the depend() perform will give us the depend of every distinctive phrase current in the father or mother string.

See the complete logic in the beneath coding snippet.

"""
Program:
 Python program to depend frequency of every phrase in a string
"""
def get_word_freq(input_string): 

   # convert the enter string into a listing of phrases
   input_string_list = input_string.cut up()     
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")
    
   unique_string_list = [] 

   # iterate the enter string listing and discover distinctive phrases 
   for i in input_string_list:         

      # check for duplicate values 
      if i not in unique_string_list: 

         # add distinctive phrases to second listing
         unique_string_list.append(i) 

   print("*******************")
   print("unique_string_list = ", unique_string_list)
   print("*******************n")
   
   print("*******************")
   for i in vary(0, len(unique_string_list)): 

      # compute phrase frequency in enter string 
      print('Word Frequency []: '.format(unique_string_list[i], input_string_list.depend(unique_string_list[i])))
    
   print("*******************")

def Driver(): 
   input_string ='python csharp javascript php python javascript csharp python csharp php'
   get_word_freq(input_string)                

if __name__=="__main__": 
   Driver()          # name Driver() perform 

The results of the above coding snippet is as follows:

*******************
input_string_list =  ['python', 'csharp', 'javascript', 'php', 'python', 'javascript', 'csharp', 'python', 'csharp', 'php']
*******************

*******************
unique_string_list =  ['python', 'csharp', 'javascript', 'php']
*******************

*******************
Word Frequency [python]: 3
Word Frequency [csharp]: 3
Word Frequency [javascript]: 2
Word Frequency [php]: 2
*******************

Sometimes, you may additionally have to convert a list to string, so have your self go over it.

Using Python set technique to get the phrase frequency

Subsequently, we will use Python’s set() perform to compute the frequency of every phrase in a string. Given beneath are some excessive-stage steps to perform the duty.

  • Again, as in the primary technique, we did the splitting of the enter string, right here additionally, now we have to do it.
  • After that, we’ll use the Python Set to take away the duplicates from the given string. In Python, the Set, by definition, has distinctive values and ignores the copies.
  • Finally, we’ll traverse over the set values and depend the occurrences of every phrase.

See the complete logic in the beneath coding snippet.

"""
Program:
 Python program to depend frequency of every phrase in a string
"""
def get_word_freq(input_string): 

   # break the string into listing of phrases 
   input_string_list = input_string.cut up() 

   # offers set of distinctive phrases 
   unique_string_set = set(input_string_list) 
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")
    
   print("*******************")
   print("unique_string_set = ", unique_string_set)
   print("*******************n")

   for entry in unique_string_set : 
      print('Frequency of ', entry , 'is :', input_string_list.depend(entry)) 

# driver code 
if __name__ == "__main__": 
   
   input_string ='python csharp javascript php python javascript csharp python csharp php'
   
   # calling the freq perform 
   get_word_freq(input_string) 

The results of the above coding snippet is as follows:

*******************
input_string_list =  ['python', 'csharp', 'javascript', 'php', 'python', 'javascript', 'csharp', 'python', 'csharp', 'php']
*******************

*******************
unique_string_set =  'csharp', 'javascript', 'python', 'php'
*******************

Frequency of  csharp is : 3
Frequency of  javascript is : 2
Frequency of  python is : 3
Frequency of  php is : 2

To study extra, learn our flagship Python tutorial for novices and superior learners.

Recommended Posts

[ad_2]

Word Frequency in a Python String | Multiple Techniqes – My programming school

Source hyperlink

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment