# Find frequency of each word in a string in Python

| |

Examples:

` Input: str [] = "Apple Mango Orange Mango Guava Guava Mango" Output: frequency of Apple is: 1 frequency of Mango is: 3 frequency of Orange is: 1 frequency of Guava is: 2 Input: str = "Train Bus Bus Train Taxi Airplane Taxi Bus" Output: frequency of Train is: 2 frequency of Bus is: 3 frequency of Taxi is: 2 frequency of Airplane is: 1 `

Approach 1 using list ():
1. Split the string into a list containing words using the split function (i.e. string.split ()) in python with space delimiter.

`  Note:   string_name.split (separator)  method is used to split the string by specified separator (delimiter) into the list. If delimiter is not provided then white space is a separator.  For example:  CODE: str = ’This is my book’ str.split () OUTPUT: [’ This’, ’is’,’ my’, ’book’] `

2. Initialize a new empty list.
3. Now add a word to the new list from the previous line if this word is not in the new list.
4. Iterate through the new list and use the count function (ie string.count (newstring [iteration])) to find the word frequency at each iteration.

`  Note:   string_name.count (substring)  is used to find no. of occurrence of substring in a given string.  For example:  CODE: str = ’Apple Mango Apple’ str.count (’ Apple’) str2 = ’Apple’ str.count (str2) OUTPUT: 2 2 `

python3

 ` # Python code to find the frequency of each word ` ` def ` ` freq (` ` str ` `) : `   ` # split the string into a list of words ` ` ` ` str ` ` = ` ` str ` `. split () ` ` ` ` str2 ` ` = ` ` [] `    ` # loop to string values ​​present in the str list ` ` for ` ` i ` ` in ` ` str ` `: `   ` # duplicate check ` ` if ` ` i ` ` not ` ` in ` ` str2: `   ` # insert value into str2 ` ` str2.append (i) ` ` `  ` ` ` for ` ` i ` ` in ` ` range ` ` (` ` 0 ` `, ` ` len ` ` (str2)): `   ` # read frequency each word (present ` ` # in str2) to str and print ` ` print ` ` (` `’ Frequency of’ ` `, str2 [i], ` ` ’is:’ ` `, ` ` str ` `. count (str2 [i])) ` `  `` def main ():   str = ’ apple mango apple orange orange apple guava mango mango’ freq ( str )    if __ name__ = = "__ main__" : main ()  # calling the main function `

Exit :

``` Frequency of apple is: 3 Frequency of mango is: 3 Frequency of orange is: 2 Frequency of guava is: 1    Approach 2 using set ():    1.  Split the string into a list containing words using the split function (i.e. string.split ()) in python with space delimiter.   2.  Use the  set () method  to remove duplicates and give a set of unique words   3.  Iterate over set and use the count function (ie string.count (newstring [iteration])) to find the word frequency in each iteration.    python3           ` # Python3 code to find the frequency of each word `  ` # function to calculate the frequency `   ` def ` ` freq (` ` str ` `): `      ` # split the string into a list of words `    ` str_list   =   str  . split () ``       # gives a set of unique words      unique_words   =   set   (str_list)        for   words   in   unique_words:     print   (  ’Frequency of’  , words,  ’ is: ’ , str_list.count (words))       # driver code     if   __ name__   =   =   "__ main__"  :        str   =  ’ apple mango apple orange orange apple guava mango mango’         # freq function call      freq (  str  ) `   Output:    Frequency of apple is: 3 Frequency of mango is: 3 Frequency of orange is: 2 Frequency of guava is: 1
Find frequency of each word in a string in Python find: Questions

Find frequency of each word in a string in Python Python functions: Questions

```

## Shop Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$ Best laptop for Zoom

\$499 Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method