首页 > 解决方案 > 进度条随列表长度更新

问题描述

我正在使用 tqdm 作为进度条。这是示例代码

from tqdm import tqdm
import numpy as np
import pandas as pd

for index_len in tqdm(range(0,len(data_elim))):

    # Doing something here where i get a array of rows which i need to delete

      data_elim = np.delete(data_elim.values,same_residues,0                      
      data_elim = pd.DataFrame(data_elim)

所以在这种情况下,我的初始长度是 17 百万,但随着它的进一步发展,它会减少并最终归零。但在我的进度条中,它始终显示为列表的初始长度。无论如何我也可以在进度栏中更改该数字吗?

标签: python-3.xprogress-bartqdm

解决方案


在不运行代码的情况下,感觉就像您可以重写它以使用tqdm“手动”模式更明确:

total_count = len(data_elim)
with tqdm(total=total_count) as progress:
  # ... do stuff, yielding `new_data`; or, calculate rows_deleted differently
  rows_deleted = total_count - len(new_data)
  progress.update(rows_deleted)

推荐阅读