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() 来获取列表元素的频率。
以上是Python程序获取单词频率的百分比的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

VS Code可以在Windows 8上运行,但体验可能不佳。首先确保系统已更新到最新补丁,然后下载与系统架构匹配的VS Code安装包,按照提示安装。安装后,注意某些扩展程序可能与Windows 8不兼容,需要寻找替代扩展或在虚拟机中使用更新的Windows系统。安装必要的扩展,检查是否正常工作。尽管VS Code在Windows 8上可行,但建议升级到更新的Windows系统以获得更好的开发体验和安全保障。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

VS Code 可用于编写 Python,并提供许多功能,使其成为开发 Python 应用程序的理想工具。它允许用户:安装 Python 扩展,以获得代码补全、语法高亮和调试等功能。使用调试器逐步跟踪代码,查找和修复错误。集成 Git,进行版本控制。使用代码格式化工具,保持代码一致性。使用 Linting 工具,提前发现潜在问题。

在 VS Code 中,可以通过以下步骤在终端运行程序:准备代码和打开集成终端确保代码目录与终端工作目录一致根据编程语言选择运行命令(如 Python 的 python your_file_name.py)检查是否成功运行并解决错误利用调试器提升调试效率

VS Code 扩展存在恶意风险,例如隐藏恶意代码、利用漏洞、伪装成合法扩展。识别恶意扩展的方法包括:检查发布者、阅读评论、检查代码、谨慎安装。安全措施还包括:安全意识、良好习惯、定期更新和杀毒软件。
