zoukankan      html  css  js  c++  java
  • tqdm模块

    tqdm 是 Python 进度条库。

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

    1.

    2.

     

    可以在 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]
  • 相关阅读:
    linux 删除乱码文件
    snprintf用法
    面试时经常问到的非技术性问题
    vector查找元素
    new 和delete
    python安装
    UIPickerView详解
    设置文本框左边显示的View
    字符串的分割??
    VC++异常捕获??
  • 原文地址:https://www.cnblogs.com/wqbin/p/11613275.html
Copyright © 2011-2022 走看看