python - tqdm - PyCharm 中带有嵌套 for 循环的多个进度条

标签 python pycharm tqdm

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

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)
但问题是,每当进度条中有更新时,内循环的进度条就会出现在换行符中,如下所示。
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]

为每个循环设置参数“位置”也不能解决问题。
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)
如何让进度条在同一行中更新?

最佳答案

解决办法有两个。

  • 转到“编辑配置”。单击正在使用的运行/调试配置。应该有一个选项“在输出控制台中模拟终端”。检查那个。添加图像以供引用。
    enter image description here
  • 随着position参数还设置了leave争论。代码应该是这样的。我已添加 ncols这样进度条就不会占据整个控制台。
  • 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)
    
    现在运行代码时,控制台的输出如下所示。
    i:  20%|████████▍                                 | 1/5 [00:05<00:20,  5.10s/it]
    j:  60%|████████████████████████▌                | 6/10 [00:03<00:02,  1.95it/s]
    
    
    更新发生在同一行。

    关于python - tqdm - PyCharm 中带有嵌套 for 循环的多个进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64727187/

    相关文章:

    python - 正则表达式。匹配以两个字母开头的所有单词

    python - South 的syncdb/migrate 创建输出页面?

    python - 更新我的 NVIDIA 版本过时错误

    python - 在 django 模板中执行 getattr() 样式查找

    python - Pycharm/IntelliJ 显示 pytest 的覆盖率为 0%,即使已生成覆盖率

    python - 我如何告诉 PyCharm 参数应该是什么类型?

    python - 属性错误 : 'tuple' object has no attribute 'encode' - MySQLdb Python

    python - tqdm progressbar 和 zip 内置不能一起工作

    python - 通过 tqdm.write() 重定向 python 脚本中的打印命令

    python - pandas.DataFrame.to_sql 的进度条