What's the difference between the two? tqdm wraps around any iterable. But I am not sure how tqdm functions when it's given two arguments.
这两者之间有什么区别?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)
1 个解决方案
#1
2
Straight from the documentation:
直接从文档:
If the optional variable total (or an iterable with len()) is provided, predictive stats are displayed.
如果提供了可选变量total(或使用len()),则会显示预测数据。
Also from the documentation:
也从文档:
total
:int
, optional .total: int,可选。
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).
期望迭代的数量。If(默认:None),如果可能,使用len(iterable)。作为最后的手段,只显示基本的进度统计信息(没有ETA,没有progressbar)。如果gui为真,此参数需要后续更新,请指定初始任意大正整数,例如int(9e9)。
When you provide total
as a parameter to tqdm
, you are giving it an estimate for how many iterations the code should take to run, so it will provide you with predictive information (even if the iterable you have provided does not have a length).
当您向tqdm提供total作为一个参数时,您将对代码应该运行多少次迭代给出一个估计,因此它将为您提供预测信息(即使您提供的iterable没有长度)。
Example
例子
If we provide a generator (something without a __len__
) to tqdm
without a total
argument, we don't get a progress bar, we just get elapsed time:
如果我们在没有完全参数的情况下为tqdm提供一个生成器(有些东西没有__len__),我们就不会获得进度条,我们只需要经过时间:
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]
However, if we use the total
parameter to give expected iterations, tqdm
will now estimate progress:
但是,如果我们使用总参数来给出预期的迭代,那么tqdm现在将评估进度:
for i in tqdm(no_len, total=49):
time.sleep(0.1)
# Result
94%|████████████████████████████████████████▎ | 46/49 [00:04<00:00, 9.72it/s
In addition to the total
parameter, tqdm
has a whole set of additional parameters that you can find here
除了总参数之外,tqdm还有一整套附加参数,您可以在这里找到。
#1
2
Straight from the documentation:
直接从文档:
If the optional variable total (or an iterable with len()) is provided, predictive stats are displayed.
如果提供了可选变量total(或使用len()),则会显示预测数据。
Also from the documentation:
也从文档:
total
:int
, optional .total: int,可选。
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).
期望迭代的数量。If(默认:None),如果可能,使用len(iterable)。作为最后的手段,只显示基本的进度统计信息(没有ETA,没有progressbar)。如果gui为真,此参数需要后续更新,请指定初始任意大正整数,例如int(9e9)。
When you provide total
as a parameter to tqdm
, you are giving it an estimate for how many iterations the code should take to run, so it will provide you with predictive information (even if the iterable you have provided does not have a length).
当您向tqdm提供total作为一个参数时,您将对代码应该运行多少次迭代给出一个估计,因此它将为您提供预测信息(即使您提供的iterable没有长度)。
Example
例子
If we provide a generator (something without a __len__
) to tqdm
without a total
argument, we don't get a progress bar, we just get elapsed time:
如果我们在没有完全参数的情况下为tqdm提供一个生成器(有些东西没有__len__),我们就不会获得进度条,我们只需要经过时间:
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]
However, if we use the total
parameter to give expected iterations, tqdm
will now estimate progress:
但是,如果我们使用总参数来给出预期的迭代,那么tqdm现在将评估进度:
for i in tqdm(no_len, total=49):
time.sleep(0.1)
# Result
94%|████████████████████████████████████████▎ | 46/49 [00:04<00:00, 9.72it/s
In addition to the total
parameter, tqdm
has a whole set of additional parameters that you can find here
除了总参数之外,tqdm还有一整套附加参数,您可以在这里找到。