gpt4 book ai didi

python - tqdm的total参数有什么作用?

转载 作者:行者123 更新时间:2023-11-28 22:18:34 31 4
gpt4 key购买 nike

两者有什么区别?tqdm 环绕任何可迭代对象。但是我不确定给定两个参数时 tqdm 是如何工作的。

# train_ids = list
elements = ('a', 'b', 'c')
for count, ele in tqdm(enumerate(elements)):
print(count, i)
# two arguments
for count, ele in tqdm(enumerate(elements), total=len(train_ids)):
print(count, i)

最佳答案

Straight from the documentation :

If the optional variable total (or an iterable with len()) is provided, predictive stats are displayed.

同样来自文档:

total : int, optional .

The number of expected iterations. If (default: None), len(iterable) is used if possible. As a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If gui is True and this parameter needs subsequent updating, specify an initial arbitrary large positive integer, e.g. int(9e9).

当您将 total 作为参数提供给 tqdm 时,您是在给它一个代码应该运行多少次迭代的估计值,因此它将为您提供预测信息(即使您提供的可迭代对象没有长度)。

示例

如果我们向 tqdm 提供一个生成器(没有 __len__ 的东西)没有 total 参数,我们没有进度条,我们只得到耗时:

no_len = (i for i in range(50))

for i in tqdm(no_len):
time.sleep(0.1)

# Result
19it [00:01, 9.68it/s]

但是,如果我们使用 total 参数给出预期的迭代次数,tqdm 现在将估计进度:

for i in tqdm(no_len, total=49):
time.sleep(0.1)

# Result
94%|████████████████████████████████████████▎ | 46/49 [00:04<00:00, 9.72it/s

除了total 参数之外,tqdm 还有一整套附加参数,您可以在here 中找到这些参数。

关于python - tqdm的total参数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50431139/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com