• tqdm python使用总结


    tqdm python使用总结

    一 概述

    参考:https://blog.csdn.net/winter2121/article/details/111356587

    总结:tqdm()只是给遍历过程添加了进度条,传入的可以是可迭代对象,也可以是不可迭代对象(需要额外设置,成为手动更新)。这两种运行模式具体见下面链接:
    https://zhuanlan.zhihu.com/p/163613814

    二 使用示例

    # 可迭代对象,自动更新
    dic = ['a', 'b', 'c', 'd', 'e']
    pbar = tqdm(dic)
    for i in pbar:
        pbar.set_description('Processing '+i)
        time.sleep(0.2)  
     
     Processing e: 100%|██████████| 5/5 [00:01<00:00,  4.69it/s]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # 不可迭代对象,手动更新
    from tqdm import tqdm
    import time
    
    with tqdm(range(100), desc='Test') as tbar:
        for i in tbar:
            tbar.set_postfix(loss=i/100, x=i)
            tbar.update()  # 手动更新,默认参数n=1,每update一次,进度+n
            time.sleep(0.2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    # 不可迭代对象,手动更新
    from tqdm import tqdm
    import time
    
    with tqdm(range(100), desc='Test') as tbar:
        for i in tbar:
            tbar.set_postfix({'loss': '{0:1.5f}'.format(i/100), "x": '{0:1.5f}'.format(i)})
            tbar.update()  # 手动更新,默认参数n=1,每update一次,进度+n
            time.sleep(0.2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    # 不可迭代对象,手动更新
    from tqdm import tqdm
    import time
    
    total = 200 #总迭代次数
    loss = total
    
    with tqdm(total=200, desc='Test') as pbar:
        pbar.set_description('Processing:') #  tqdm()中的desc参数和pbar.set_description()是一回事,后者优先。
        for i in range(20):
            loss -= 1
            pbar.set_postfix({'loss': '{0:1.5f}'.format(i/100), "x": '{0:1.5f}'.format(i)})  # 输入一个字典,显示实验指标
            # pbar.set_postfix(loss=i/100, x=i)
            pbar.update(10)  # 手动更新,默认参数n=1,每update一次,进度+n
            time.sleep(0.2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    总结:tqdm.set_postfix()传入参数的两种方式:

    # 1 传入元组
    tbar.set_postfix(loss=i/100, x=i)
    
    # 2 传入字典
    tbar.set_postfix({'loss': '{0:1.5f}'.format(i/100), "x": '{0:1.5f}'.format(i)})
    
    # 两种方法的结果一样
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    单商户社区团购系统小程序源码
    【实战】React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(总结展望篇)
    SMT贴片制造:专业、现代、智能的未来之选
    优步让一切人工智能化
    java不同类加载器重复加载一个类
    【let var const】
    One bite of Stream(5)
    元素水平垂直居中
    (new online judge)1322蓝桥杯2017初赛 包子凑数
    关于cmake --build .的理解
  • 原文地址:https://blog.csdn.net/LIWEI940638093/article/details/126694282