tqdm

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,294评论 4 61
  • FMDB是轻量级的数据库.在ios中使用十分常见 FMDB的官方源代码github地址是https://githu...
    AAup阅读 267评论 0 1
  • 对方就像是一面镜子,让你看清楚自己。 分手后是怎样的感觉?是撕心裂肺的痛吧,因为曾经拥有过,爱过。 遇见总是双方的...
    啦啦啦是晨晨啊阅读 317评论 1 3
  • 一 海岛一年四季草杂树杂,什么奇奇怪怪的草木都有...
    老茹_e0a5阅读 996评论 0 0
  • 有很多人没事了都喜欢挖挖鼻子,甚至挖鼻成瘾想戒都戒不掉。特别是带娃的宝妈们都会发现,如果自己爱挖鼻子的话,宝宝也会...
    涵涵妈育儿阅读 391评论 0 1