Python 中的双进度条

2024-01-09

有没有办法在Python中创建双进度条? 我想在彼此内部运行两个循环。对于每个循环,我想要一个进度条。我的程序看起来像:

import time
for i1 in range(5):
    for i2 in range(300):
        # do something, e.g. sleep
        time.sleep(0.01)
        # update upper progress bar
    # update lower progress bar

中间某处的输出应该类似于

50%|############################                                  |ETA: 0:00:02
80%|##################################################            |ETA: 0:00:04

已经存在的真的很酷进度条 http://code.google.com/p/python-progressbar/模块似乎不支持这一点。


Use the tqdm 的嵌套进度条功能 https://github.com/tqdm/tqdm#nested-progress-bars,一个开销极低、可高度定制的进度条库:

$ pip install -U tqdm

Then:

from tqdm import tqdm
# from tqdm.auto import tqdm  # notebook compatible
import time
for i1 in tqdm(range(5)):
    for i2 in tqdm(range(300), leave=False):
        # do something, e.g. sleep
        time.sleep(0.01)

(The leave=False是可选的 - 需要在完成后丢弃嵌套栏。)

您还可以使用from tqdm import trange然后替换tqdm(range(...)) with trange(...)。你也可以得到它在笔记本上工作 https://github.com/tqdm/tqdm#ipython-jupyter-integration.

或者,如果您只想用一个栏来监控所有内容,您可以使用tqdm的版本itertools.product:

from tqdm.contrib import itertools
import time
for i1, i2 in itertools.product(range(5), range(300)):
    # do something, e.g. sleep
    time.sleep(0.01)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 中的双进度条 的相关文章

随机推荐