tqdm

编程入门 行业动态 更新时间:2024-10-27 11:18:06
本文介绍了tqdm - PyCharm 中带有嵌套 for 循环的多个进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下问题适用于使用 PyCharm 的人.有嵌套的 for 循环,tqdm 用于每个 for 循环对应的进度条.代码如下所示.

The below question is for people who use PyCharm. There are nested for loops and tqdm is used for progress bars corresponding to each for loop. The code is shown below.

from tqdm import tqdm import time for i in tqdm(range(5), desc="i", colour='green'): for j in tqdm(range(10), desc="j", colour='red'): time.sleep(0.5)

但问题是,每次更新时,内部循环的进度条都会出现在换行符中,如下所示.

But the problem is, the progress bars for the inner loop appear in newline everytime there is an update in the bar as shown below.

i: 0%| | 0/5 [00:00<?, ?it/s] j: 0%| | 0/10 [00:00<?, ?it/s] j: 10%|█ | 1/10 [00:00<00:04, 1.94it/s] j: 20%|██ | 2/10 [00:01<00:04, 1.94it/s] j: 30%|███ | 3/10 [00:01<00:03, 1.96it/s] j: 40%|████ | 4/10 [00:02<00:03, 1.96it/s] j: 50%|█████ | 5/10 [00:02<00:02, 1.97it/s] j: 60%|██████ | 6/10 [00:03<00:02, 1.97it/s] j: 70%|███████ | 7/10 [00:03<00:01, 1.97it/s] j: 80%|████████ | 8/10 [00:04<00:01, 1.98it/s] j: 90%|█████████ | 9/10 [00:04<00:00, 1.98it/s] j: 100%|██████████| 10/10 [00:05<00:00, 1.98it/s] i: 20%|██ | 1/5 [00:05<00:20, 5.06s/it] j: 0%| | 0/10 [00:00<?, ?it/s] j: 10%|█ | 1/10 [00:00<00:04, 2.00it/s] j: 20%|██ | 2/10 [00:01<00:04, 1.99it/s] j: 30%|███ | 3/10 [00:01<00:03, 1.99it/s] j: 40%|████ | 4/10 [00:02<00:03, 1.99it/s] j: 50%|█████ | 5/10 [00:02<00:02, 1.99it/s] j: 60%|██████ | 6/10 [00:03<00:02, 1.99it/s] j: 70%|███████ | 7/10 [00:03<00:01, 1.99it/s] j: 80%|████████ | 8/10 [00:04<00:01, 1.99it/s] j: 90%|█████████ | 9/10 [00:04<00:00, 1.99it/s] j: 100%|██████████| 10/10 [00:05<00:00, 1.99it/s] i: 40%|████ | 2/5 [00:10<00:15, 5.05s/it]

为每个循环设置参数位置"也不能解决问题.

Setting the parameter 'position` for each loop also doesn't fix the issue.

from tqdm import tqdm import time for i in tqdm(range(5), desc="i", colour='green', position=0): for j in tqdm(range(10), desc="j", colour='red', position=1): time.sleep(0.5)

如何让进度条在同一行更新?

How does one get the progress bar to update in the same line?

推荐答案

解决方案有两个.

  • 转到编辑配置".单击正在使用的运行/调试配置.应该有一个选项在输出控制台中模拟终端".检查那个.添加图片以供参考.

  • Go to "Edit configurations". Click on the run/debug configuration that is being used. There should be an option "Emulate terminal in output console". Check that. Image added for reference.

    与 position 参数一起,还设置 leave 参数.代码应如下所示.我添加了 ncols 以便进度条不会占据整个控制台.

    Along with the position argument also set the leave argument. The code should look like this. I have added ncols so that the progress bar doesn't take up whole of the console.

    from tqdm import tqdm import time for i in tqdm(range(5), position=0, desc="i", leave=False, colour='green', ncols=80): for j in tqdm(range(10), position=1, desc="j", leave=False, colour='red', ncols=80): time.sleep(0.5)

    现在运行代码时,控制台的输出如下图所示.

    When the code is now run, the output of the console is as shown below.

    i: 20%|████████▍ | 1/5 [00:05<00:20, 5.10s/it] j: 60%|████████████████████████▌ | 6/10 [00:03<00:02, 1.95it/s]

    更新发生在同一行.

  • 更多推荐

    tqdm

    本文发布于:2023-11-23 09:50:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1621005.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:tqdm

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!