好玩的Python库tqdm (转载)
Python 转载 好玩
2023-09-11 14:19:20 时间
原文地址:
https://blog.csdn.net/zejianli/article/details/77915751
可以显示循环的进度条的库,再也不用担心不知道程序跑到哪里还要跑多久了
tqdm()可以直接包裹iterable的对象
from tqdm import tqdm,trange from time import sleep text = "" for char in tqdm(["a", "b", "c", "d"]): text = text + char sleep(0.1)
trange(i)相当于tqdm(range(i))
for i in trange(100): sleep(0.01)
可以在循环外面预先定义tqdm的对象
pbar = tqdm(["a", "b", "c", "d"]) for char in pbar: pbar.set_description("Processing %s" % char)
有两个参数比较有用,desc(str)和leave(bool)
desc可以指定这个循环的的信息,以便区分。上面的set_description(str)和这个应该是一样的。
leave则表示进度条跑完了之后是否继续保留
for i in tqdm(range(10), desc='1st loop'): for j in trange(100, desc='2nd loop', leave=False): sleep(0.01)
如果要在Jupyter Notebook上面使用,那么要把tqdm换成tqdm_notebook,trange换成tnrange
from tqdm import tnrange, tqdm_notebook
from time import sleep
for i in tqdm_notebook(range(10), desc='1st loop'):
for j in tnrange(100, desc='2nd loop', leave=False):
sleep(0.01)
如果在终端运行时需要在循环内打印点什么,就不能直接用print()了,而要用tqdm.write(str) 。
在Jupyter Notebook里面没有这个问题,还能用print()。
=======================================================
另注(非原文内容):
desc(str) 指定循环信息是比较好用的,如:
from tqdm import trange, tqdm from time import sleep for i in tqdm(range(100), desc='1st loop'): sleep(1)
实际操作中发现 desc(str) 比 set_description 好用。
相关文章
- 91个改善Python程序的小技巧(转载)
- 【Python】模块之queue
- 用Python输出一个杨辉三角的例子
- Python jieba库简介和使用
- [Py] Python 的 shape、reshape 含义与用法
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(LabelFrame/Checkbutton/Radiobutton)
- 100天精通Python(可视化篇)——第83天:matplotlib绘制不同种类炫酷箱形图参数说明+代码实战(水平、缺口、群组、堆叠、核密度、小提琴箱形图)
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- 已解决(Python安装报错)Visit python.org to download an earlier version of Python.
- Python 实现个人博客系统(附零基础python学习资料)
- Python:看我如何全程自动玩游戏带你飞,直接无敌
- Python中的层次聚类,详细讲解
- 【Python成长之路】python 基础篇 -- 装饰器【华为云分享】
- Python:导入numpy报错 No module named numpy
- 雅虎财经数据python 网络爬虫stock股票 用 Python 通过雅虎财经获取股票数据
- python自动化测试学习-Python测试框架之unittest和pytest
- Python 头部 #!/usr/bin/python 和 #!/usr/bin/env 的区别
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- 转载:大厂5G python自动化测试面试必会 | 匿名函数lambda & 递归函数 & 函数属性和注解 & 函数式编程工具