python tqdm模块的安装和使用

版权声明:站在巨人的肩膀上学习。 https://blog.csdn.net/zgcr654321/article/details/84960203

tqdm是一个快速,可扩展的Python进度条,可以在Python的for循环中添加一个进度提示信息,进度条可以针对任意迭代器对象。

tqdm模块的安装:

python -m pip install tqdm

tqdm模块的使用:

最简单的用法:显示for循环的进度,并计算已用时间/剩余时间:

import tqdm
import time

count = 0
for i in tqdm.tqdm(range(100)):
	count += i
	time.sleep(0.05)

tqdm(range(i)) 也可以写成trange(i) 。

import tqdm
import time

count = 0
for i in tqdm.trange(100):
	count += i
	time.sleep(0.05)

运行结果如下:

100%|██████████| 100/100 [00:05<00:00, 20.00it/s]

Process finished with exit code 0

我们也可以给进度条加上标题:

import tqdm
import time

count = 0
x = tqdm.tqdm(range(100))
for i in x:
	count += i
	time.sleep(0.05)
	x.set_description("现在是第{}轮".format(i))

运行结果如下:

现在是第99轮: 100%|██████████| 100/100 [00:05<00:00, 20.00it/s]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/zgcr654321/article/details/84960203
今日推荐