Python程序:计算单词频率百分比


在这篇文章中,我们将学习如何在Python中计算单词频率百分比。

假设我们已经输入了一个字符串列表。现在我们将找到给定输入字符串列表中每个单词的百分比。

公式

(Occurrence of X word / Total words) * 100

使用的方法

  • 使用sum(),Counter(),join()和split()函数

  • 使用join(),split()和count()函数

  • 使用operator模块中的countOf()函数。

方法1:使用sum(),Counter(),join()和split()函数

join()是Python中的一个字符串函数,用于连接由字符串分隔符分隔的序列元素。此函数连接序列元素以形成一个字符串。

Counter()函数是一个子类,用于计数可哈希的对象。它在调用/调用时隐式地创建一个可迭代对象的哈希表。

算法(步骤)

以下是执行所需任务的算法/步骤。

  • 使用import关键字从collections模块导入Counter函数。

  • 创建一个变量来存储字符串的输入列表并打印列表。

  • 使用join()函数连接输入列表的所有字符串元素。

  • 使用split()函数(将字符串分割成列表。我们可以定义分隔符;默认分隔符是任何空格)将连接的字符串分割成单词列表,并使用Counter()函数获取单词频率作为键值对。

  • 使用values()函数从Counter获取所有值(频率/计数),并使用sum()函数(返回可迭代对象中所有项目的总和)获取它们的总和。

  • 使用items()函数(返回一个视图对象,即它包含字典的键值对,作为列表中的元组)从上面的计数器单词获取每个单词的百分比。

  • 打印输入列表中每个单词的百分比。

示例

以下程序使用sum()、Counter()、join()和split()函数返回给定输入字符串列表中每个单词的百分比。

Open Compiler
# importing a Counter function from the collections module from collections import Counter # input list of strings inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"] print("Input list:\n", inputList) # Joining all the string elements of the list using the join() function join_string = " ".join(i for i in inputList) # splitting the joined string into a list of words and getting the # frequency of words as key-value pairs using Counter() function counter_words = Counter(join_string.split()) # getting all the values(frequencies/counts) from counter and # finding the total sum of them total_sum = sum(counter_words.values()) # getting the percentage of each word from the above counter words res_percentage = {key: value / total_sum for key, value in counter_words.items()} # printing the percentage of each word from the input list print("Percentage of each word from the input list:\n", res_percentage)

输出

执行后,上述程序将生成以下输出:

Input list:
['hello tutorialspoint', 'python codes', 'tutorialspoint for python', 'see python codes tutorialspoint']
Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

方法2:使用join(),split()和count()函数

算法(步骤)

以下是执行所需任务的算法/步骤。

  • 创建一个空字典用于存储结果百分比/单词频率。

  • 使用for循环遍历单词列表。

  • 使用if条件语句,使用keys()函数检查当前元素是否不在字典的键中。

  • 如果上述条件为真,则使用count()函数获取此键(单词)的计数。

  • 将其除以单词数以获取当前单词频率,并将其作为键存储在上面创建的新字典中。

  • 打印输入列表中每个单词的百分比。

示例

以下程序使用join()、split()和count()函数返回给定输入字符串列表中每个单词的百分比。

Open Compiler
# input list of strings inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"] # joining all the elements of the list using join() join_string = " ".join(i for i in inputList) # splitting the joined string into a list of words listOfWords = join_string.split() # Creating an empty dictionary for storing the resultant percentages resDict = dict() # traversing through the list of words for item in listOfWords: # checking whether the current element is not in the keys of a dictionary if item not in resDict.keys(): # getting the percentage of a current word if the condition is true resDict[item] = listOfWords.count(item)/len(listOfWords) # printing the percentage of each word from the input list print("Percentage of each word from the input list:\n", resDict)

输出

执行后,上述程序将生成以下输出:

Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

方法3:使用operator模块中的countOf()函数

示例

以下程序使用countOf()函数返回给定输入字符串列表中每个单词的百分比。

Open Compiler
import operator as op # input list of strings inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"] # joining all the elements of list using join() join_string = " ".join(i for i in inputList) # splitting the joined string into list of words listOfWords = join_string.split() resDict = dict() for item in listOfWords: # checking whether the current element is not in the keys of dictionary if item not in resDict.keys(): resDict[item] = op.countOf(listOfWords, item)/len(listOfWords) print("Percentage of each word from the input list:\n", resDict)

输出

执行后,上述程序将生成以下输出:

Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

结论

在这篇文章中,我们学习了三种不同的Python方法来计算单词频率百分比。我们还学习了如何使用operator模块的新函数countOf()来获取列表元素的频率。

更新于:2023年1月27日

600 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告