编写程序,统计下面英文短文中,每个单词出现的次数。其他要求:(1)忽略大小写;(2)去除标点符号,不能在单词中出现标点或出现对标点的统计;(3)按词频由高到低的顺序输出统计结果,每个词及其统计结果显示时固定宽度并右对齐,每行显示5个单词的统计结果,总体要求整齐,方便查看,参考代码行数:35行
文章:
In our world , one creature without any rivals is a lifeless creature. If a man lives without rivals, he is bound to be satisfied with the present and will not strive for the better. He would hold back before all difficulties and decline in inaction and laziness. Adverse environment tends to cultivate successful people. Therefore, your rivals are not your opponents or those you grudge. Instead , they are your good friends! In our lives, we need some rivals to "push us into the river", leaving us striving ahead in all difficulties and competitions. In our work, we need some rivals to be picky about us and supervise our work with rigorous requirements and standards. Due to our rivals, we can bring out our potential to the best; Due to our rivals, we will continuously promote our capabilities when competing with them!
预期效果:
python实现词频统计问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 游一游走一走 2022-10-21 22:54关注
import re data = 'In our world , one creature without any rivals is a lifeless creature. If a man lives without rivals, he is bound to be satisfied with the present and will not strive for the better. He would hold back before all difficulties and decline in inaction and laziness. Adverse environment tends to cultivate successful people. Therefore, your rivals are not your opponents or those you grudge. Instead , they are your good friends! In our lives, we need some rivals to "push us into the river", leaving us striving ahead in all difficulties and competitions. In our work, we need some rivals to be picky about us and supervise our work with rigorous requirements and standards. Due to our rivals, we can bring out our potential to the best; Due to our rivals, we will continuously promote our capabilities when competing with them!' data = data.lower() rule = re.compile("[^a-zA-Z]") data = rule.sub(' ', data) setWords = data.split(' ') result = {} for item in setWords: if item == ' ' or item == '': continue result[item] = result.get(item, 0) + 1 result = sorted(result.items(), key=lambda x: x[1], reverse=True) for index, item in enumerate(result, 1): print('{}->{}'.format(item[0].rjust(20, ' '), item[1]), end='') if index % 5 == 0: print()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 Matlab计算100000*100000的矩阵运算问题:
- ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
- ¥16 需要完整的这份订单所有的代码,可以加钱
- ¥30 写一个带界面控制的机房电脑一键开机关机并且实时监控的软件
- ¥15 Stata数据分析请教
- ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
- ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料
- ¥15 51单片机的外部中断,按下按键后不能切换到另一个模式
- ¥15 java连接sqlserver有问题
- ¥15 yolov8 如何调cfg参数