卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18410本站已运行340

Python程序获取单词频率的百分比

Python程序获取单词频率的百分比

在本文中,我们将学习如何在Python中以百分比形式获取词频。

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

公式

'
(Occurrence of X word / Total words) * 100

使用的方法

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

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

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

方法一:使用 sum()、Counter()、join() 和 split() 函数

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

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

算法(步骤)

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

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

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

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

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

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

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

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

Example

的中文翻译为:

示例

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

'
# 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()函数获取该键(单词)的计数。

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

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

Example

的中文翻译为:

示例

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

'
# 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}

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

Example

的中文翻译为:

示例

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

'
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 方法来计算百分比词频。我们还学习了如何使用操作符模块的新函数 countOf() 来获取列表元素的频率。

卓越飞翔博客
上一篇: PHP开发者的职业生涯与高薪发展
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏