tqdm python summary

tqdm python summary

I. Overview

Reference: https://blog.csdn.net/winter2121/article/details/111356587

Summary: tqdm() just adds a progress bar to the traversal process, and the incoming object can be iterable or non-iterable (requires additional settings and becomes manual update). For the details of these two operating modes, see the link below:
https://zhuanlan.zhihu.com/p/163613814

Two use examples

# 可迭代对象,自动更新
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]
# 不可迭代对象,手动更新
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)

insert image description here

# 不可迭代对象,手动更新
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)

insert image description here

# 不可迭代对象,手动更新
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)

insert image description here
Summary: There are two ways for tqdm.set_postfix() to pass in parameters:

# 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)})

# 两种方法的结果一样

Guess you like

Origin blog.csdn.net/LIWEI940638093/article/details/126694282