搜索
首页后端开发Python教程Python程序获取单词频率的百分比

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

Sep 08, 2023 pm 06:29 PM
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() 来获取列表元素的频率。

以上是Python程序获取单词频率的百分比的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
学习Python:2小时的每日学习是否足够?学习Python:2小时的每日学习是否足够?Apr 18, 2025 am 12:22 AM

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Web开发的Python:关键应用程序Web开发的Python:关键应用程序Apr 18, 2025 am 12:20 AM

Python在Web开发中的关键应用包括使用Django和Flask框架、API开发、数据分析与可视化、机器学习与AI、以及性能优化。1.Django和Flask框架:Django适合快速开发复杂应用,Flask适用于小型或高度自定义项目。2.API开发:使用Flask或DjangoRESTFramework构建RESTfulAPI。3.数据分析与可视化:利用Python处理数据并通过Web界面展示。4.机器学习与AI:Python用于构建智能Web应用。5.性能优化:通过异步编程、缓存和代码优

Python vs.C:探索性能和效率Python vs.C:探索性能和效率Apr 18, 2025 am 12:20 AM

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

python在行动中:现实世界中的例子python在行动中:现实世界中的例子Apr 18, 2025 am 12:18 AM

Python在现实世界中的应用包括数据分析、Web开发、人工智能和自动化。1)在数据分析中,Python使用Pandas和Matplotlib处理和可视化数据。2)Web开发中,Django和Flask框架简化了Web应用的创建。3)人工智能领域,TensorFlow和PyTorch用于构建和训练模型。4)自动化方面,Python脚本可用于复制文件等任务。

Python的主要用途:综合概述Python的主要用途:综合概述Apr 18, 2025 am 12:18 AM

Python在数据科学、Web开发和自动化脚本领域广泛应用。1)在数据科学中,Python通过NumPy、Pandas等库简化数据处理和分析。2)在Web开发中,Django和Flask框架使开发者能快速构建应用。3)在自动化脚本中,Python的简洁性和标准库使其成为理想选择。

Python的主要目的:灵活性和易用性Python的主要目的:灵活性和易用性Apr 17, 2025 am 12:14 AM

Python的灵活性体现在多范式支持和动态类型系统,易用性则源于语法简洁和丰富的标准库。1.灵活性:支持面向对象、函数式和过程式编程,动态类型系统提高开发效率。2.易用性:语法接近自然语言,标准库涵盖广泛功能,简化开发过程。

Python:多功能编程的力量Python:多功能编程的力量Apr 17, 2025 am 12:09 AM

Python因其简洁与强大而备受青睐,适用于从初学者到高级开发者的各种需求。其多功能性体现在:1)易学易用,语法简单;2)丰富的库和框架,如NumPy、Pandas等;3)跨平台支持,可在多种操作系统上运行;4)适合脚本和自动化任务,提升工作效率。

每天2小时学习Python:实用指南每天2小时学习Python:实用指南Apr 17, 2025 am 12:05 AM

可以,在每天花费两个小时的时间内学会Python。1.制定合理的学习计划,2.选择合适的学习资源,3.通过实践巩固所学知识,这些步骤能帮助你在短时间内掌握Python。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器