以下问题适用于使用 PyCharm 的人。
存在嵌套的 for
循环,并且对于每个 for
循环都使用了 tqdm
来显示进度条。代码如下所示。
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]
为每个循环设置参数“position”也无法解决问题。
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)
如何在同一行中更新进度条?