统计文本中的数字出现频次

75 篇文章 0 订阅

统计文本中的数字出现频次:磁盘上有多个文本文件,统计数据写入excel。


  (本笔记适合初通 Python 的 coder 翻阅)


【学习的细节是欢悦的历程】


  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……


磁盘上有多个文本文件
统计文本中的数字出现频次
(统计数据写入excel)


本文质量分:

96
本文地址: https://blog.csdn.net/m0_57158496/article/details/131508571

CSDN质量分查询入口:http://www.csdn.net/qc


目 录


◆ 统计文本中的数字出现频次


1、题目描述


题目来源于 CSDN 问答社区提问“统计多个文本中的数字出现频次


  遍历多个*.txt文本列表,依次统计数字出现频次,将统计以*.txt为标签追加写入目标文本文件。由于我的python 环境没有成功pip pandas ,就把统计结果写入csv文本文件了。

回页目录

2、解题思路


2.1 数据文本准备


  没有文本数据操练代码?祭出python 的“随机大法器”random,用choice()方法随机在给定范围选数100写入文本,连写10文本,搞定操练数据!😋


  • 随机生成十个数字文本
    在这里插入图片描述
    在这里插入图片描述

  • python 代码

#!/sur/bin/nve python
# coding: utf-8
from random import choice


mypath = '/sdcard/001/num001/'

def write_numfile(filename):
    ''' 随机写入100个整数 '''
    nums = range(5001)
    
    with open(filename, 'w') as f:
        
        for i in range(100):
            f.write(f"{choice(nums)}\n") 


def main():
    
    for i in range(1, 11): # 生成10随机文本文件。
        write_numfile(f"{mypath}{i:0>3}.txt")



if __name__ == '__main__':
    main()



回页目录

2.2 遍历轮询统计文本中的数字出现频次


  遍历轮询统计文本文件中的数字出现频次:依次遍历每个文件中的各个数据以数字为key,出现频次为value 的字典统计,不停的累加出现的数字,直到遍历完整个文本中的数字。列表解析字典的统计数据,按出现频次排降序,依次写入csv文本。(我的这python 环境没有pip成功写操作excel文件的库,所以仅用csv格式写入文本)


  • 循环遍历轮询文本文件,统计数字生成的tsv文本文件
    在这里插入图片描述

  • tsv文本内容
    在这里插入图片描述

  • python 代码


def count_num(filename):
    ''' 统计文本中的数字 '''
    
    with open(filename) as f: # 读取文本。
        text = f.read()[:-1]

    count_dict = {} # 数字统计字典。
    for i in text.split('\n'): # 遍历轮询文本数字统计出现频次。
        count_dict[i] = count_dict.get(i, 0) + 1
    
    count = [(num, count) for num,count in count_dict.items()] # 列表解析统计字典数据。
    count.sort(reverse=True, key=lambda x: x[1])
    
    with open(f"{filename[:-4]}_count.txt", 'w') as f:
        f.write(f"数字,出现频次")
        
        for num,k in count:
            f.write(f"\n{num},{k}")

def main():
    
    for i in range(1, 11): # 生成10随机文本文件。
        write_numfile(f"{mypath}{i:0>3}.txt")

    for i in range(1, 11): # 生成10随机文本文件。
        count_num(f"{mypath}{i:0>3}.txt")


if __name__ == '__main__':
    main()




回页目录

3、完整源码

(源码较长,点此跳过源码)

#!/sur/bin/nve python
# coding: utf-8
from random import choice


mypath = '/sdcard/001/num001/'

def write_numfile(filename):
    ''' 随机写入100个整数 '''
    nums = range(201)
    
    with open(filename, 'w') as f:
        
        for i in range(100):
            f.write(f"{choice(nums)}\n")


def count_num(filename):
    ''' 统计文本中的数字 '''
    
    with open(filename) as f: # 读取文本。
        text = f.read()[:-1]

    count_dict = {} # 数字统计字典。
    for i in text.split('\n'): # 遍历轮询文本数字统计出现频次。
        count_dict[i] = count_dict.get(i, 0) + 1
    
    count = [(num, count) for num,count in count_dict.items()] # 列表解析统计字典数据。
    count.sort(reverse=True, key=lambda x: x[1])
    
    with open(f"{filename[:-4]}_count.txt", 'w') as f:
        f.write(f"数字,出现频次")
        
        for num,k in count:
            f.write(f"\n{num},{k}")


def main():
    
    for i in range(1, 11): # 生成10随机文本文件。
        write_numfile(f"{mypath}{i:0>3}.txt")

    for i in range(1, 11): # 生成10随机文本文件。
        count_num(f"{mypath}{i:0>3}.txt")


if __name__ == '__main__':
    main()


回页首

上一篇:  学号编码:TooY0ung的学院(结构体)(根据6+6十二位编码规则,用城市代码和出生年编制学号)
下一篇: 

我的HOT博:

  本次共计收集 220 篇博文笔记信息,总阅读量 31.17w,平均阅读量 1416。已生成 21 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-07-02 22:58:06 完成,用时 5 分 26.73 秒。


  1. 让QQ群昵称色变的神奇代码
    ( 56405 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500
    点赞:24   踩 :0  收藏:81  打赏:0  评论:17
    本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。
  2. pandas 数据类型之 DataFrame
    ( 8763 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814
    点赞:6   踩 :0  收藏:31  打赏:0  评论:0
    本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。
  3. 个人信息提取(字符串)
    ( 6928 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618
    点赞:1   踩 :0  收藏:12  打赏:0  评论:0
    本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。
  4. 罗马数字转换器|罗马数字生成器
    ( 6657 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。
  5. Python字符串居中显示
    ( 6573 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023
    点赞:1   踩 :0  收藏:6  打赏:0  评论:1
    本篇博文笔记于 2021-12-26 23:35:29 发布。
  6. Python列表(list)反序(降序)的7种实现方式
    ( 5617 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700
    点赞:4   踩 :0  收藏:18  打赏:0  评论:8
    本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。
  7. 斐波那契数列的递归实现和for实现
    ( 5382 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295
    点赞:4   踩 :0  收藏:2  打赏:0  评论:8
    本篇博文笔记于 2022-01-06 23:27:40 发布。
  8. 练习:字符串统计(坑:f‘string‘报错)
    ( 4965 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2021-12-04 22:54:29 发布。
  9. 练习:尼姆游戏(聪明版/傻瓜式•人机对战)
    ( 4722 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399
    点赞:14   踩 :0  收藏:42  打赏:0  评论:0
    本篇博文笔记于 2021-11-30 23:43:17 发布。
  10. python清屏
    ( 4695 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101
    点赞:0   踩 :0  收藏:5  打赏:0  评论:0
    本篇博文笔记于 2021-10-14 13:47:21 发布。
  11. 回车符、换行符和回车换行符
    ( 4645 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488
    点赞:1   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。
  12. 练习:生成100个随机正整数
    ( 4051 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220
    点赞:1   踩 :0  收藏:6  打赏:0  评论:0
    本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。
  13. 密码强度检测器
    ( 4042 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。
  14. 罗马数字转换器(用罗马数字构造元素的值取模实现)
    ( 3925 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。
  15. 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法)
    ( 3725 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935
    点赞:1   踩 :0  收藏:3  打赏:0  评论:0
    本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。
  16. 我的 Python.color() (Python 色彩打印控制)
    ( 3701 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259
    点赞:2   踩 :0  收藏:7  打赏:0  评论:0
    本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。
  17. 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
    ( 3432 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626
    点赞:3   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。
  18. random.sample()将在python 3.9x后续版本中被弃用
    ( 3265 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120657230
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2021-10-08 18:35:09 发布。
  19. 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) )
    ( 3253 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589
    点赞:3   踩 :0  收藏:2  打赏:0  评论:3
    本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。
  20. Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义
    ( 3230 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606
    点赞:0   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。
  21. 练习:求列表(整数列表)平衡点
    ( 3104 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2021-12-05 23:28:10 发布。
推荐条件 阅读量突破三千
(更多热博,请点击蓝色文字跳转翻阅)

回页首


老齐漫画头像

精品文章:

来源:老齐教室


回页首

Python 入门指南【Python 3.6.3】


好文力荐:


CSDN实用技巧博文:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值