Change language

# 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

```

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

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

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries