tqdm模块[通俗易懂]

tqdm模块[通俗易懂]tqdm是Python进度条库。tqdm库下面有2个类我们经常使用:1.2.可以在Python长循环中添加一个进度提示信息用法:tqdm(iterator)trange(i)是

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

tqdm 是 Python 进度条库。

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

1.

tqdm模块[通俗易懂]

 

 

2.

 tqdm模块[通俗易懂]

 

 

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

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

可以总结为三个方法:

方法一:

# 方法1:
import time
from tqdm import tqdm  

for i in tqdm(range(100)):  
    time.sleep(0.01)

方法1+:
import time
from tqdm import trange

for i in trange(100):
    time.sleep(0.01) 

结果如下:

 0%| | 0/100 [00:00<?, ?it/s] 10%|█ | 10/100 [00:00<00:00, 94.10it/s] 20%|██ | 20/100 [00:00<00:00, 93.77it/s] 30%|███ | 30/100 [00:00<00:00, 93.71it/s] 40%|████ | 40/100 [00:00<00:00, 93.49it/s] 50%|█████ | 50/100 [00:00<00:00, 93.56it/s] 60%|██████ | 60/100 [00:00<00:00, 92.82it/s] 70%|███████ | 70/100 [00:00<00:00, 92.57it/s] 80%|████████ | 80/100 [00:00<00:00, 92.44it/s] 90%|█████████ | 90/100 [00:00<00:00, 92.82it/s] 100%|██████████| 100/100 [00:01<00:00, 92.81it/s] 100%|██████████| 100/100 [00:01<00:00, 92.91it/s]
0%| | 0/100 [00:00<?, ?it/s] 10%|█ | 10/100 [00:00<00:00, 93.74it/s] 20%|██ | 20/100 [00:00<00:00, 93.20it/s] 30%|███ | 30/100 [00:00<00:00, 92.83it/s] 40%|████ | 40/100 [00:00<00:00, 92.83it/s] 50%|█████ | 50/100 [00:00<00:00, 92.57it/s] 60%|██████ | 60/100 [00:00<00:00, 92.90it/s] 70%|███████ | 70/100 [00:00<00:00, 92.88it/s] 80%|████████ | 80/100 [00:00<00:00, 93.00it/s] 90%|█████████ | 90/100 [00:00<00:00, 92.69it/s] 100%|██████████| 100/100 [00:01<00:00, 92.76it/s] 100%|██████████| 100/100 [00:01<00:00, 92.71it/s]

 

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

 

import time from tqdm import tqdm pbar = tqdm(["a", "b", "c", "d"]) for char in pbar: # 设置描述 pbar.set_description("Processing %s" % char) time.sleep(0.2)

 

 0%| | 0/4 [00:00<?, ?it/s] Processing a: 0%| | 0/4 [00:00<?, ?it/s] Processing a: 25%|██▌ | 1/4 [00:00<00:00, 4.99it/s] Processing b: 25%|██▌ | 1/4 [00:00<00:00, 4.99it/s] Processing b: 50%|█████ | 2/4 [00:00<00:00, 4.99it/s] Processing c: 50%|█████ | 2/4 [00:00<00:00, 4.99it/s] Processing c: 75%|███████▌ | 3/4 [00:00<00:00, 4.99it/s] Processing d: 75%|███████▌ | 3/4 [00:00<00:00, 4.99it/s] Processing d: 100%|██████████| 4/4 [00:00<00:00, 4.99it/s] Processing d: 100%|██████████| 4/4 [00:00<00:00, 4.99it/s]

 

方法三:手动更新

import time from tqdm import tqdm # 一共200个,每次更新10,一共更新20次 with tqdm(total=200) as pbar: pbar.set_description("Processing") for i in range(20): pbar.update(10) time.sleep(0.1) #方法2: pbar = tqdm(total=200) for i in range(20): pbar.update(10) time.sleep(0.1) pbar.close()
 0%| | 0/200 [00:00<?, ?it/s] Processing: 0%| | 0/200 [00:00<?, ?it/s] Processing: 10%|█ | 20/200 [00:00<00:00, 198.53it/s] Processing: 15%|█▌ | 30/200 [00:00<00:01, 152.68it/s] Processing: 20%|██ | 40/200 [00:00<00:01, 131.50it/s] Processing: 25%|██▌ | 50/200 [00:00<00:01, 119.83it/s] Processing: 30%|███ | 60/200 [00:00<00:01, 112.82it/s] Processing: 35%|███▌ | 70/200 [00:00<00:01, 108.39it/s] Processing: 40%|████ | 80/200 [00:00<00:01, 105.48it/s] Processing: 45%|████▌ | 90/200 [00:00<00:01, 103.54it/s] Processing: 50%|█████ | 100/200 [00:00<00:00, 102.21it/s] Processing: 55%|█████▌ | 110/200 [00:01<00:00, 101.32it/s] Processing: 60%|██████ | 120/200 [00:01<00:00, 100.70it/s] Processing: 65%|██████▌ | 130/200 [00:01<00:00, 100.27it/s] Processing: 70%|███████ | 140/200 [00:01<00:00, 100.17it/s] Processing: 75%|███████▌ | 150/200 [00:01<00:00, 100.00it/s] Processing: 80%|████████ | 160/200 [00:01<00:00, 99.78it/s] Processing: 85%|████████▌ | 170/200 [00:01<00:00, 99.75it/s] Processing: 90%|█████████ | 180/200 [00:01<00:00, 99.60it/s] Processing: 95%|█████████▌| 190/200 [00:01<00:00, 99.71it/s] Processing: 100%|██████████| 200/200 [00:01<00:00, 99.68it/s] Processing: 100%|██████████| 200/200 [00:02<00:00, 99.39it/s] 

0
%| | 0/200 [00:00<?, ?it/s] 10%|█ | 20/200 [00:00<00:00, 198.60it/s] 15%|█▌ | 30/200 [00:00<00:01, 152.73it/s] 20%|██ | 40/200 [00:00<00:01, 131.51it/s] 25%|██▌ | 50/200 [00:00<00:01, 119.83it/s] 30%|███ | 60/200 [00:00<00:01, 112.82it/s] 35%|███▌ | 70/200 [00:00<00:01, 108.38it/s] 40%|████ | 80/200 [00:00<00:01, 105.37it/s] 45%|████▌ | 90/200 [00:00<00:01, 103.56it/s] 50%|█████ | 100/200 [00:00<00:00, 102.19it/s] 55%|█████▌ | 110/200 [00:01<00:00, 101.52it/s] 60%|██████ | 120/200 [00:01<00:00, 100.93it/s] 65%|██████▌ | 130/200 [00:01<00:00, 100.43it/s] 70%|███████ | 140/200 [00:01<00:00, 100.08it/s] 75%|███████▌ | 150/200 [00:01<00:00, 100.04it/s] 80%|████████ | 160/200 [00:01<00:00, 99.90it/s] 85%|████████▌ | 170/200 [00:01<00:00, 99.92it/s] 90%|█████████ | 180/200 [00:01<00:00, 99.81it/s] 95%|█████████▌| 190/200 [00:01<00:00, 99.86it/s] 100%|██████████| 200/200 [00:01<00:00, 99.78it/s] 100%|██████████| 200/200 [00:02<00:00, 99.47it/s]

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/166511.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号