TQDM在块中处理序列的进度

发布于 2025-02-05 11:41:32 字数 1190 浏览 3 评论 0原文

我正在处理块中的一个序列,其中最后一个块可能更短,并且想显示进度栏显示项目数量。 当然,直接的方法是

import tqdm, math
total=567
chunkSize=100
# each pass process items i0…max(i0+chunkSize,total)
for i0 in tqdm.tqdm(range(0,total,chunkSize)): pass

显示块的数量,而不是项目的数量:

100%|█████████████████████████████████| 6/6 [00:00<00:00, 75121.86it/s]

更好的选择

for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=chunkSize,total=total/chunkSize): pass
for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=float(chunkSize),total=total/chunkSize): pass
for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=chunkSize,total=math.ceil(total/chunkSize)): pass

分别是:

106%|██████████████████████████████████| 600.0/567.0 [00:00<00:00, 6006163.25it/s]
106%|██████████████████████████████████| 600/567.0 [00:00<00:00, 5264816.74it/s]
100%|██████████████████████████████████| 600/600 [00:00<00:00, 4721542.96it/s]

在100%超过100%

tqdm/std.py:533: TqdmWarning: clamping frac to range [0, 1]

的情况下,我需要的是进度栏,这将显示项目的数量(不是块),正确的百分比,也将正确显示最大值,而不是圆形的大小。想法?

I am processing a sequence in chunks, where the last chunk may be shorter, and would like to show progress bar showing the number of items. The straightforward approach is

import tqdm, math
total=567
chunkSize=100
# each pass process items i0…max(i0+chunkSize,total)
for i0 in tqdm.tqdm(range(0,total,chunkSize)): pass

resulting in showing the number of chunks, not of the items, of course:

100%|█████████████████████████████████| 6/6 [00:00<00:00, 75121.86it/s]

Somewhat better options are

for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=chunkSize,total=total/chunkSize): pass
for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=float(chunkSize),total=total/chunkSize): pass
for i0 in tqdm.tqdm(range(0,total,chunkSize),unit_scale=chunkSize,total=math.ceil(total/chunkSize)): pass

which respectively give:

106%|██████████████████████████████████| 600.0/567.0 [00:00<00:00, 6006163.25it/s]
106%|██████████████████████████████████| 600/567.0 [00:00<00:00, 5264816.74it/s]
100%|██████████████████████████████████| 600/600 [00:00<00:00, 4721542.96it/s]

where those going over 100% show understandably

tqdm/std.py:533: TqdmWarning: clamping frac to range [0, 1]

So what I need is progress bar which will show the number of items (not chunks), correct percentages and will also correctly show the max value, not rounded to the chunk size. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2025-02-12 11:41:32

块尺寸可变?可以用

import tqdm
total = 567
chunkSize = 100

with tqdm.tqdm(total=total) as pbar:
    # each pass process items i0…min(i0 + chunkSize, total)
    for i0 in range(0, total, chunkSize):
        end = min(i0 + chunkSize, total)
        do_something(start=i0, end=end)
        pbar.update(end - i0)

Variable chunk size? Could handle this manually with tqdm.tqdm.update:

import tqdm
total = 567
chunkSize = 100

with tqdm.tqdm(total=total) as pbar:
    # each pass process items i0…min(i0 + chunkSize, total)
    for i0 in range(0, total, chunkSize):
        end = min(i0 + chunkSize, total)
        do_something(start=i0, end=end)
        pbar.update(end - i0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文