python tqdm进度条打印
python tqdm进度条打印
使用tqdm来进行迭代可以打印进度条,tqdm中的tqdm()是实现进度条美化的基本方法,在for循环体中用tqdm()包裹指定的迭代器或range()。
简单展示:
from tqdm import tqdm
import time
a=[1,2,3,4,5]
b=[6,7,8,9,10]
c = (1,2,3,4,5,6)
d={1:'a',2:'b',3:'c',4:'d',5:'f'}
e = ((1,2),(3,4),(5,6),(7,8),(9,10))
for i in tqdm(a):
pass
#time.sleep(1)
for i in tqdm(range(5)):
pass
#time.sleep(1)
for i in tqdm(c):
pass
#time.sleep(1)
for i in tqdm(d):
pass
#time.sleep(1)
for i in tqdm(e):
pass
for i,j in tqdm(enumerate(a)):
pass
for i,j in tqdm(zip(a,b)):
pass
#time.sleep(1)
结果:
100%|██████████| 5/5 [00:00<?, ?it/s]
100%|██████████| 5/5 [00:00<?, ?it/s]
100%|██████████| 6/6 [00:00<?, ?it/s]
100%|██████████| 5/5 [00:00<?, ?it/s]
100%|██████████| 5/5 [00:00<?, ?it/s]
5it [00:00, ?it/s]
5it [00:00, ?it/s]
大部分可迭代对象都能打印进度条,不过迭代的是两个元素的时候,好像不能打印进度条,不过还是能够获取其进度的。