获取tqdm中的后缀字符串



我有一个tqdm进度条。我在代码的某些部分使用方法set_postfix_str设置了后缀字符串。在另一部分中,我需要附加到这个字符串。这是MWE。

import numpy as np
from tqdm import tqdm
a = np.random.randint(0, 10, 10)
loop_obj = tqdm(np.arange(10))
for i in loop_obj:
loop_obj.set_postfix_str(f"Current count: {i}")
a = i*2/3  # Do some operations
loop_obj.set_postfix_str(f"After processing: {a}")  # clears the previous string

# What I want
loop_obj.set_postfix_str(f"Current count: {i}After processing: {a}")

有没有一种方法可以使用set_postfix_str附加到已经设置的字符串?

您可以将新的后缀附加到旧的后缀上,如下所示:

import numpy as np
from tqdm import tqdm
a = np.random.randint(0, 10, 10)
loop_obj = tqdm(np.arange(10))
for i in loop_obj:
loop_obj.set_postfix_str(f"Current count: {i}")
a = i*2/3  # Do some operations
loop_obj.set_postfix_str(loop_obj.postfix + f" After processing: {a}")

最新更新