首页 > 其他 > 详细

tqdm——进度条模块

时间:2019-12-12 11:54:18      阅读:133      评论:0      收藏:0      [点我收藏+]

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

# 方法1:
import time
from tqdm import tqdm  

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

方法2:
import time
from tqdm import trange

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

结果:

 0%|          | 0/100 [00:00<?, ?it/s]
 11%|█         | 11/100 [00:00<00:00, 100.00it/s]
 22%|██▏       | 22/100 [00:00<00:00, 100.00it/s]
 32%|███▏      | 32/100 [00:00<00:00, 100.00it/s]
 43%|████▎     | 43/100 [00:00<00:00, 100.00it/s]
 54%|█████▍    | 54/100 [00:00<00:00, 100.00it/s]
 64%|██████▍   | 64/100 [00:00<00:00, 99.11it/s] 
 74%|███████▍  | 74/100 [00:00<00:00, 99.37it/s]
 85%|████████▌ | 85/100 [00:00<00:00, 99.56it/s]
 95%|█████████▌| 95/100 [00:00<00:00, 99.69it/s]
100%|██████████| 100/100 [00:01<00:00, 99.70it/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(1)

结果:

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

手动控制进度:

import time
from tqdm import tqdm

# 一共200个,每次更新10,一共更新20次
with tqdm(total=200) as pbar:
  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)
# close() 不要也没出问题?
pbar.close()

结果:

  0%|          | 0/200 [00:00<?, ?it/s]
 15%|█▌        | 30/200 [00:00<00:01, 150.00it/s]
 25%|██▌       | 50/200 [00:00<00:01, 130.43it/s]
 30%|███       | 60/200 [00:00<00:01, 119.52it/s]
 40%|████      | 80/200 [00:00<00:01, 112.91it/s]
 50%|█████     | 100/200 [00:00<00:00, 108.70it/s]
 55%|█████▌    | 110/200 [00:01<00:00, 105.93it/s]
 65%|██████▌   | 130/200 [00:01<00:00, 104.08it/s]
 75%|███████▌  | 150/200 [00:01<00:00, 102.82it/s]
 80%|████████  | 160/200 [00:01<00:00, 101.96it/s]
 85%|████████▌ | 170/200 [00:01<00:00, 96.38it/s] 
 90%|█████████ | 180/200 [00:01<00:00, 97.44it/s]
100%|██████████| 200/200 [00:01<00:00, 98.19it/s]

更多用法,学习完后再补充:
https://blog.csdn.net/langb2014/article/details/54798823?locationnum=8&fps=1



作者:持续积累
链接:https://www.jianshu.com/p/21cf48be6bf6
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

tqdm——进度条模块

原文:https://www.cnblogs.com/GavinSimons/p/12027877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!