tqdm 是 Python 进度条库。

tqdm库下面有2个类我们经常使用:

1.

2.

可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator)

trange(i) 是 tqdm(range(i)) 的简单写法。

可以总结为三个方法:

方法一:

  1. # 方法1:
  2. import time
  3. from tqdm import tqdm
  4.  
  5. for i in tqdm(range()):
  6. time.sleep(0.01)
  7.  
  8. 方法1+:
  9. import time
  10. from tqdm import trange
  11.  
  12. for i in trange():
  13. time.sleep(0.01)

结果如下:

  1. %| | / [:<?, ?it/s]
  2. %|█ | / [:<:, .10it/s]
  3. %|██ | / [:<:, .77it/s]
  4. %|███ | / [:<:, .71it/s]
  5. %|████ | / [:<:, .49it/s]
  6. %|█████ | / [:<:, .56it/s]
  7. %|██████ | / [:<:, .82it/s]
  8. %|███████ | / [:<:, .57it/s]
  9. %|████████ | / [:<:, .44it/s]
  10. %|█████████ | / [:<:, .82it/s]
  11. %|██████████| / [:<:, .81it/s]
  12. %|██████████| / [:<:, .91it/s]
  13. %| | / [:<?, ?it/s]
  14. %|█ | / [:<:, .74it/s]
  15. %|██ | / [:<:, .20it/s]
  16. %|███ | / [:<:, .83it/s]
  17. %|████ | / [:<:, .83it/s]
  18. %|█████ | / [:<:, .57it/s]
  19. %|██████ | / [:<:, .90it/s]
  20. %|███████ | / [:<:, .88it/s]
  21. %|████████ | / [:<:, .00it/s]
  22. %|█████████ | / [:<:, .69it/s]
  23. %|██████████| / [:<:, .76it/s]
  24. %|██████████| / [:<:, .71it/s]

方法二:可以为进度条设置描述

  1. import time
  2. from tqdm import tqdm
  3.  
  4. pbar = tqdm(["a", "b", "c", "d"])
  5. for char in pbar:
  6. # 设置描述
  7. pbar.set_description("Processing %s" % char)
  8. time.sleep(0.2)
  1. %| | / [:<?, ?it/s]
  2. Processing a: %| | / [:<?, ?it/s]
  3. Processing a: %|██▌ | / [:<:, .99it/s]
  4. Processing b: %|██▌ | / [:<:, .99it/s]
  5. Processing b: %|█████ | / [:<:, .99it/s]
  6. Processing c: %|█████ | / [:<:, .99it/s]
  7. Processing c: %|███████▌ | / [:<:, .99it/s]
  8. Processing d: %|███████▌ | / [:<:, .99it/s]
  9. Processing d: %|██████████| / [:<:, .99it/s]
  10. Processing d: %|██████████| / [:<:, .99it/s]

方法三:手动更新

  1. import time
  2. from tqdm import tqdm
  3.  
  4. # 一共200个,每次更新10,一共更新20次
  5. with tqdm(total=200) as pbar:
  6. pbar.set_description("Processing")
  7. for i in range(20):
  8. pbar.update(10)
  9. time.sleep(0.1)
  10.  
  11. #方法2:
  12. pbar = tqdm(total=200)
  13. for i in range(20):
  14. pbar.update(10)
  15. time.sleep(0.1)
  16. pbar.close()
  1. 0%| | 0/200 [00:00<?, ?it/s]
  2. Processing: 0%| | 0/200 [00:00<?, ?it/s]
  3. Processing: 10%|█ | 20/200 [00:00<00:00, 198.53it/s]
  4. Processing: 15%|█▌ | 30/200 [00:00<00:01, 152.68it/s]
  5. Processing: 20%|██ | 40/200 [00:00<00:01, 131.50it/s]
  6. Processing: 25%|██▌ | 50/200 [00:00<00:01, 119.83it/s]
  7. Processing: 30%|███ | 60/200 [00:00<00:01, 112.82it/s]
  8. Processing: 35%|███▌ | 70/200 [00:00<00:01, 108.39it/s]
  9. Processing: 40%|████ | 80/200 [00:00<00:01, 105.48it/s]
  10. Processing: 45%|████▌ | 90/200 [00:00<00:01, 103.54it/s]
  11. Processing: 50%|█████ | 100/200 [00:00<00:00, 102.21it/s]
  12. Processing: 55%|█████▌ | 110/200 [00:01<00:00, 101.32it/s]
  13. Processing: 60%|██████ | 120/200 [00:01<00:00, 100.70it/s]
  14. Processing: 65%|██████▌ | 130/200 [00:01<00:00, 100.27it/s]
  15. Processing: 70%|███████ | 140/200 [00:01<00:00, 100.17it/s]
  16. Processing: 75%|███████▌ | 150/200 [00:01<00:00, 100.00it/s]
  17. Processing: 80%|████████ | 160/200 [00:01<00:00, 99.78it/s]
  18. Processing: 85%|████████▌ | 170/200 [00:01<00:00, 99.75it/s]
  19. Processing: 90%|█████████ | 180/200 [00:01<00:00, 99.60it/s]
  20. Processing: 95%|█████████▌| 190/200 [00:01<00:00, 99.71it/s]
  21. Processing: 100%|██████████| 200/200 [00:01<00:00, 99.68it/s]
  22. Processing: 100%|██████████| 200/200 [00:02<00:00, 99.39it/s]
  23.  
  24. 0%| | 0/200 [00:00<?, ?it/s]
  25. 10%|█ | 20/200 [00:00<00:00, 198.60it/s]
  26. 15%|█▌ | 30/200 [00:00<00:01, 152.73it/s]
  27. 20%|██ | 40/200 [00:00<00:01, 131.51it/s]
  28. 25%|██▌ | 50/200 [00:00<00:01, 119.83it/s]
  29. 30%|███ | 60/200 [00:00<00:01, 112.82it/s]
  30. 35%|███▌ | 70/200 [00:00<00:01, 108.38it/s]
  31. 40%|████ | 80/200 [00:00<00:01, 105.37it/s]
  32. 45%|████▌ | 90/200 [00:00<00:01, 103.56it/s]
  33. 50%|█████ | 100/200 [00:00<00:00, 102.19it/s]
  34. 55%|█████▌ | 110/200 [00:01<00:00, 101.52it/s]
  35. 60%|██████ | 120/200 [00:01<00:00, 100.93it/s]
  36. 65%|██████▌ | 130/200 [00:01<00:00, 100.43it/s]
  37. 70%|███████ | 140/200 [00:01<00:00, 100.08it/s]
  38. 75%|███████▌ | 150/200 [00:01<00:00, 100.04it/s]
  39. 80%|████████ | 160/200 [00:01<00:00, 99.90it/s]
  40. 85%|████████▌ | 170/200 [00:01<00:00, 99.92it/s]
  41. 90%|█████████ | 180/200 [00:01<00:00, 99.81it/s]
  42. 95%|█████████▌| 190/200 [00:01<00:00, 99.86it/s]
  43. 100%|██████████| 200/200 [00:01<00:00, 99.78it/s]
  44. 100%|██████████| 200/200 [00:02<00:00, 99.47it/s]

tqdm模块的更多相关文章

  1. hdf5文件、tqdm模块、nunique、read_csv、sort_values、astype、fillna

    pandas.DataFrame.to_hdf(self, path_or_buf, key, **kwargs): Hierarchical Data Format (HDF) ,to add an ...

  2. [Python]-tqdm模块-给for循环加上进度条

    import tqdm 使用tqdm模块,可以在漫长的for循环加上一个进度条,显示当前进度百分比. 将tqdm写在迭代器之外即可:tqdm(iterator) for i in tqdm(range ...

  3. (数据科学学习手札53)Python中tqdm模块的用法

    一.简介 tqdm是Python中专门用于进度条美化的模块,通过在非while的循环体内嵌入tqdm,可以得到一个能更好展现程序运行过程的提示进度条,本文就将针对tqdm的基本用法进行介绍. 二.基本 ...

  4. python的tqdm模块

    Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator). 根据要求安装依赖即可. 可以很方便的在 ...

  5. python的tqdm模块介绍

    https://www.jianshu.com/p/b27318efdb7b Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) ...

  6. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...

  7. 万能的Python,还能用来制作高大上的进度条?

    对于开发或者运维来说,使用Python去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效的监控任务的进度,除了在任务中加上log外,还能不能有另一种方式来了解任务进展到哪一步了呢? ...

  8. 【Python】给程序加个进度条

    对于开发或者运维来说,使用 Python 去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效地监控任务的进度?除了在任务中加上 Log 外,还能不能有另一种方式来了解任务进展到哪一步 ...

  9. 用 Python 给程序加个进度条,让你的看起来更炫酷?

    对于开发或者运维来说,使用 Python 去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效地监控任务的进度?除了在任务中加上 Log 外,还能不能有另一种方式来了解任务进展到哪一步 ...

随机推荐

  1. 【1.1】mysql frm文件丢失(ibd文件丢失)

    [1]故障模拟准备环境 这里以innodb为例 [1.1]配置参数 开启独立表空间 innodb_file_per_table; [1.2]构建测试数据 create database test; c ...

  2. Shell脚本中计算字符串长度的5种方法

    有时在Linux操作系统中需要计算某个字符串的长度,通过查询资料整理了下目前Shell中获取字符串的长度的多种方法,在这里分享给大家,方法如下: 方法1: 使用wc -L命令wc -L可以获取到当前行 ...

  3. 服务器:消息18456,级别16,状态1 用户‘sa’登录失败解决方法

    无法连接到服务器**:  服务器:消息18456,级别16,状态1   [Microsoft][ODBC   SQL   Server   Driver][Sql   server]   用户 'sa ...

  4. Yii2.0 组件

    框架之所以是框架,是因为其强大,其封装了很多实用的功能,开发者可以开箱即用. 下边列举Yii2.0的部分组件: var_dump(Yii::$app->session->getId()); ...

  5. 在Window Server 2016中使用Web Deploy方式发布.NET Web应用

    1.在IIS里面点击获取新的Web平台组件 2.下载Web平台组件并安装 3.在其中搜索Web Deploy,找到3.5版本,并安装 4.继续搜索Web Deploy 3.6版本,并安装 安装好之后, ...

  6. OnMouseWheel的通常处理

    BOOL CMainWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint point) { BOOL bUp = TRUE; int nDelt ...

  7. axios配置

    import axios, { isCancel } from 'axios' import { md5 } from 'vux' import util from '@/libs/util' imp ...

  8. 在Global.asax中 注册Application_Error事件 捕获全局异常

    参考于:https://shiyousan.com/post/635813858052755170 在ASP.NET MVC中,通过应用程序生命周期中的Application_Error事件可以捕获到 ...

  9. BZOJ4241历史研究题解--回滚莫队

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4241 分析 这题就是求区间权值乘以权值出现次数的最大值,一看莫队法块可搞,但仔细想想,莫 ...

  10. LeetCode:595.大的国家

    题目链接:https://leetcode-cn.com/problems/big-countries/ 题目 这里有张 World 表 +-----------------+------------ ...