【问题标题】:tqdm: update total without resetting time elapsedtqdm:更新总数而不重置已用时间
【发布时间】:2021-04-17 12:18:54
【问题描述】:

我在递归目录树时使用 tqdm。我不知道我将使用的路径数,我不想在我做工作之前建立那个列表只是为了得到一个准确的总数,我宁愿让它更新进度条为它会继续。

我发现我可以很好地使用“reset(total=new_total)”,但这也会重置时间。有没有办法让我保持时间,但只需将总数设置为新的?

【问题讨论】:

    标签: python tqdm


    【解决方案1】:

    这里是tqdm包内的reset函数定义:

        def reset(self, total=None):
            """
            Resets to 0 iterations for repeated use.
    
            Consider combining with `leave=True`.
    
            Parameters
            ----------
            total  : int, optional. Total to use for the new bar.
            """
            self.last_print_n = self.n = 0
            self.last_print_t = self.start_t = self._time()
            if total is not None:
                self.total = total
            self.refresh()
    

    您需要的不是更新self.last_print_tself.start_t 的值,而是更新total

    您应该执行以下操作,而不是调用t.reset(total=new_total)

    t.total = new_total
    t.refresh()
    

    【讨论】:

    • 太棒了;那行得通。现在,如果我只能找出如何用句点替换默认块字符... :) 谢谢!
    • 它确实修复了经过的时间,但是在那之后未来的估计时间对我来说是零。
    • 有效,但在笔记本中,进度条未更新。
    猜你喜欢
    • 2019-11-02
    • 2021-07-09
    • 1970-01-01
    • 2019-06-22
    • 2011-02-20
    • 2023-03-10
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多