python模块tqdm(进度条)的使用

放几个样例:
1.

pbar = tqdm(range(1000))

for idx, num in  enumerate(pbar):
    # print(num)
    pbar.set_description('下载速度 %i' % idx)
    pbar.set_postfix(loss=random(), gen=randint(1,999), str='详细信息',
                     lst=[1, 2])
from tqdm import tqdm

# assume
dataset_num = 100000
batch_size = 8
epoch = 1
d_loss = 1.2345
g_loss = 1.2345

pbar = tqdm(range(int(dataset_num / batch_size)))

for i in pbar:
    epoch += 1
    d_loss += 1.2345
    g_loss += 1.2345
    pbar.set_description('Epoch: %i' % epoch)
    pbar.set_postfix(d_loss=format(d_loss,'.3f'), g_loss=format(g_loss,'.3f'))

这样可以在tqdm读条的前后显示需要的信息,set_description()设定的是前缀,set_postfix()设定的是后缀。
如果需要更加细致全面美观的功能,可以使用progress模块。使用tqdm主要就是图个方便省事。

参考链接:

  1. tqdm pbar.set_postfix使用
  2. Python保留指定位数的小数
原文地址:https://www.cnblogs.com/lfri/p/15709969.html