在Python中查找字符串中每个单词的频率

2021年4月14日14:29:51 发表评论 670 次浏览

本文概述

编写python代码以查找给定字符串中每个单词的频率。

例子:

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 Aeroplane Taxi Bus"
Output : frequency of Train is : 2
         frequency of Bus is : 3
         frequency of Taxi is : 2
         frequency of Aeroplane is : 1

使用list()的方法1:

1.通过在python中使用带分隔符的分隔功能(即string.split())将字符串分成包含单词的列表。

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.初始化一个新的空列表。

3.现在, 如果新列表中不存在该单词, 则将该单词附加到先前字符串中的新列表中。

4.遍历新列表, 并使用计数函数(即string.count(newstring [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 frequency of each word
def freq( str ):
  
     # break the string into list of words 
     str = str .split()         
     str2 = []
  
     # loop till string values present in list str
     for i in str :             
  
         # checking for the duplicacy
         if i not in str2:
  
             # insert value in str2
             str2.append(i) 
              
     for i in range ( 0 , len (str2)):
  
         # count the frequency of each word(present 
         # in str2) in 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()             # call main function

输出如下:

Frequency of  apple  is :  3
Frequency of  mango  is :  3
Frequency of  orange  is :  2
Frequency of  guava  is :  1

使用set()的方法2:

1.通过在Python中使用带分隔符的分隔功能(即string.split())将字符串分成包含单词的列表。

2.采用set()删除重复项并给出一组唯一单词的方法

3.遍历set并使用count函数(即string.count(newstring [iteration]))来查找每次迭代中单词的出现频率。

Python3

# Python3 code to find frequency of each word
# function for calculating the frequency
def freq( str ):
  
     # break the string into list of words
     str_list = str .split()
  
     # gives 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'
      
     # calling the freq function
     freq( str )

输出如下:

Frequency of  apple  is :  3
Frequency of  mango  is :  3
Frequency of  orange  is :  2
Frequency of  guava  is :  1

方法3(使用字典)

# Find frequency of each word in a string in Python
# using dictionary.
   
def count(elements):
     # check if each word has '.' at its last. If so then ignore '.'
     if elements[ - 1 ] = = '.' :
         elements = elements[ 0 : len (elements) - 1 ]
   
     # if there exists a key as "elements" then simply
     # increase its value.
     if elements in dictionary:
         dictionary[elements] + = 1
   
     # if the dictionary does not have the key as "elements" 
     # then create a key "elements" and assign its value to 1.
     else :
         dictionary.update({elements: 1 })
   
   
# driver input to check the program.
   
Sentence = "Apple Mango Orange Mango Guava Guava Mango"
   
# Declare a dictionary
dictionary = {}
   
# split all the word of the string.
lst = Sentence.split()
   
# take each word from lst and pass it to the method count.
for elements in lst:
     count(elements)
   
# print the keys and its corresponding values.
for allKeys in dictionary:
     print ( "Frequency of " , allKeys, end = " " )
     print ( ":" , end = " " )
     print (dictionary[allKeys], end = " " )
     print () 
   
# This code is contributed by Ronit Shrivastava.

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: