0

0

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

王林

王林

发布时间:2023-09-08 18:29:02

|

2060人浏览过

|

来源于tutorialspoint

转载

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

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

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

公式

(Occurrence of X word / Total words) * 100

使用的方法

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

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

    立即学习Python免费学习笔记(深入)”;

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

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

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

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

算法(步骤)

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

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

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

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

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

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

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

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

    BgSub
    BgSub

    免费的AI图片背景去除工具

    下载

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速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

715

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

625

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

739

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

617

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1235

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

575

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

698

2023.08.11

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Django 教程
Django 教程

共28课时 | 2.6万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号